Home >Backend Development >Python Tutorial >How can I create threads in Python using functions without subclassing Thread?
Creating Threads in Python Using Functions
Problem:
To run two functions simultaneously in a Python script, you're unable to implement threading using the provided example code. You prefer to use a threaded function instead of a class-based approach.
Solution:
You can create threads using threaded functions in Python without using a subclass of Thread. Here's an example:
<code class="python">from threading import Thread from time import sleep def threaded_function(arg): for i in range(arg): print("running") sleep(1) if __name__ == "__main__": thread = Thread(target=threaded_function, args=(10,)) thread.start() thread.join() print("thread finished...exiting")</code>
Explanation:
The above is the detailed content of How can I create threads in Python using functions without subclassing Thread?. For more information, please follow other related articles on the PHP Chinese website!