The threading library can be used to execute any Python callable in its own thread. To do this, create a Thread instance and supply the callable that you wish to execute as a target as shown in the code given below –
When a thread instance is created, it doesn’t start executing until its
start() method (which invokes the target function with the arguments you supplied) is invoked. Threads are executed in their own system-level thread (e.g., a POSIX thread or Windows threads) that is fully managed by the host operating system. Once started, threads run independently until the target function returns.
Code #2 : Querying a thread instance to see if it’s still running.
One can also request to join with a thread, which waits for it to terminate.
The interpreter remains running until all threads terminate. For long-running threads or background tasks that run forever, consider it making the thread daemonic.
Code #3 :
Daemonic threads can’t be joined. However, they are destroyed automatically when the main thread terminates. Beyond the two operations shown, there aren’t many other things to do with threads. For example, there are no operations to terminate a thread, signal a thread, adjust its scheduling, or perform any other high-level operations. To have these features, build them on their own. To be able to terminate threads, the thread must be programmed to poll for
exit at selected points. For example, put your thread in a class such as the one mentioned in the code below –
Code #4 : Putting the thread in a class.
Polling for thread termination can be tricky to coordinate if threads perform blocking operations such as I/O. For example, a thread blocked indefinitely on an I/O operation may never return to check if it’s been killed. To correctly deal with this case, thread needs to be carefully programmed to utilize timeout loops as shown in the code given below.
Code #5 :
Due to a global interpreter lock (GIL), Python threads are restricted to an execution model that only allows one thread to execute in the interpreter at any given time. For this reason, Python threads should generally not be used for computationally intensive tasks where trying to achieve parallelism on multiple CPUs. They are much better suited for I/O handling and handling concurrent execution in code that performs blocking operations (e.g., waiting for I/O, waiting for results from a database, etc.).
Code #6 : Threads defined via inheritance from the Thread class
Although this works, it introduces an extra dependency between the code and the threading library. That is, only the resulting code can be used in the context of threads, whereas the technique shown earlier involves writing code with no explicit dependency on threading. By freeing your code of such dependencies, it becomes usable in other contexts that may or may not involve threads. For instance, one might be able to execute the code in a separate process using the multiprocessing module using code given below –
Code #7 :
Again, this only works if the
CountdownTask class has been written in a manner that is neutral to the actual means of concurrency (threads, processes, etc).
- Removing stop words with NLTK in Python
- Part of Speech Tagging with Stop words using NLTK in python
- Python | Different ways to kill a Thread
- Check if a Thread has started in Python
- Thread-based parallelism in Python
- Python | Ways to remove n characters from start of given string
- How to Start Learning Machine Learning?
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.