Most programs are executed line by line, only running a single process at a time. Threads allow multiple processes to flow independent of each other. Threading with multiple processors permits programs to run multiple processes simultaneously. This topic documents the implementation and usage of threads in Python.
Global Interpreter Lock
Python multithreading performance can often suffer due to the Global Interpreter Lock. In short, even though you can have multiple threads in a Python program, only one bytecode instruction can execute in parallel at any one time, regardless of the number of CPUs.
As such, multithreading in cases where operations are blocked by external events - like network access - can be quite effective:
Note that even though each process took 2 seconds to execute, the four processes together were able to effectively run in parallel, taking 2 seconds total.
However, multithreading in cases where intensive computations are being done in Python code - such as a lot of computation - does not result in much improvement, and can even be slower than running in parallel:
In the latter case, multiprocessing can be effective as multiple processes can, of course, execute multiple instructions simultaneously:
Running in Multiple Processes
Use multiprocessing.Process to run a function in another process. The interface is similar to threading.Thread:
Running in Multiple Threads
Use threading.Thread to run a function in another thread.
Sharing State Between Processes
Code running in different processes do not, by default, share the same data. However, the multiprocessing module contains primitives to help share values across multiple processes.
Sharing State Between Threads
As all threads are running in the same process, all threads have access to the same data.
However, concurrent access to shared data should be protected with a lock to avoid synchronization issues.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0