Python has long supported different approaches to concurrent programming, including programming with threads, launching subprocesses, and various tricks involving generator functions. In this chapter, recipes related to various aspects of concurrent programming are presented, including common thread programming techniques and approaches for parallel processing.
As experienced programmers know, concurrent programming is fraught with potential peril. Thus, a major focus of this chapter is on recipes that tend to lead to more reliable and debuggable code.
You want to create and destroy threads for concurrent execution of code.
threading library can be used to execute any Python callable
in its own thread. To do this, you create a
Thread instance and
supply the callable that you wish to execute as a target. Here is a
# Code to execute in an independent thread
# Create and launch a thread
When you create a thread instance, it doesn’t start executing until
you invoke its
start() method (which invokes the target function
with the arguments you supplied).
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 ...