How do I use threading in Python?
Employ Python's built-in threading
module by extending the Thread
class and then defining the run
method. This method contains the code which will be executed in parallel. Start the thread using the start()
method. An illustration is as follows:
This code snippet creates a basic thread to print "Parallel execution underway". join()
ensures the main program waits for the thread to finish.
An overview of threads and multiprocessing.dummy
Threads in Python are optimal for use in I/O-bound tasks; situations where the system spends a large proportion of its time waiting for external resources or inputs. Python’s multiprocessing.dummy.Pool
is powerful and provides a pool of threads for executing function calls in parallel:
For CPU-bound tasks, tasks restricted by your CPU's processing power, go with the multiprocessing
module to take advantage of process-based parallelism.
Starting and maintaining threads
The threading.Thread()
function is an alternative way to create threads by directly using, rather than subclassing. Here's one way to do it:
You can prevent threads from blocking the main program from exiting using t.daemon = True
to create daemon threads.
Synchronizing threads
Python implements the Queue.Queue()
class for secure data sharing and synchronization among threads. Managing shared resources among threads prevents race conditions:
Working around Python's GIL
Python’s GIL (Global Interpreter Lock) can be problematic for multi-threaded CPU-bound programs because it only allows one thread to control the Python interpreter at one time. This is why understanding the differentiation between multithreading and multiprocessing is crucial. In the context of CPU-heavy tasks, choose multiprocessing
to bypass the GIL.
When to use threads
Go for threads for I/O-bound or network-bound operations. An example is making HTTP requests using .urlopen()
:
Threads with multiple arguments
From Python 3.3 onwards, multiprocessing.Pool.starmap()
helps deal with multiple arguments for parallel tasks:
Including itertools.repeat()
allows for repeating constants:
Thread synchronization methods
Python provides advanced thread synchronization tools, including semaphores, locks, and events. They can manage complex inter-thread conduct and prevent threads from colliding.
Considerations in multithreading
Python threads may decrease performance in CPU-bound tasks due to context switches. Processes often yield better results for non I/O-bound tasks.
Safety precautions
When using multiprocessing
, it's usual to wrap your starting code in if __name__ == '__main__':
to avoid unintended behavior.
Was this article helpful?