Python Concurrency: Multi-threading and Multi-processing
In Python, ThreadPoolExecutor and ProcessPoolExecutor are part of the concurrent.futures module, which provides a high-level interface for asynchronously executing callables. These executors are designed to simplify the use of threads and processes for parallel execution of tasks.
ThreadPoolExecutor Example
The ThreadPoolExecutor is used for executing tasks in a pool of threads. It’s suitable for I/O-bound tasks and when you want to limit the number of threads that run concurrently.
1 | |
In this example:
- We define a simple function
taskthat simulates a task. - We use
ThreadPoolExecutorwith a maximum of 5 worker threads. - We submit 10 tasks to the executor.
- We use
as_completedto retrieve the results as they are completed.
ProcessPoolExecutor Example
The ProcessPoolExecutor is ideal for CPU-bound tasks and leverages multiple processes, bypassing the Global Interpreter Lock (GIL) in CPython.
1 | |
In this example:
- We define a
computefunction that performs a CPU-intensive task. - We create a
ProcessPoolExecutorwith 4 worker processes. - We submit 4 compute tasks to the executor.
- We collect and print the results as they are completed.
Points to Note
- The
max_workersparameter defines the maximum number of threads or processes that the executor can use. Adjust it based on your task and the available resources. - The
submitmethod schedules the callable to be executed and returns aFutureobject, representing the execution’s eventual result. - It’s important to use the
withstatement to ensure that the resources are properly cleaned up after the execution. - For
ProcessPoolExecutor, keep in mind that each process has its own memory space, so communication between processes is more costly compared to threads.
Python Concurrency: Multi-threading and Multi-processing
https://www.hardyhu.cn/2024/01/29/Python-Concurrency-Multi-threading-and-Multi-processing/