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).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course