Junior — Middle
What methods are implemented in Python for executing tasks in parallel or concurrently?
sobes.tech AI
Answer from AI
In Python, several approaches and libraries are used for parallel or concurrent tasks:
-
threadingmodule — for creating threads. Suitable for I/O-bound tasks, but does not speed up CPU-bound tasks due to GIL. -
multiprocessingmodule — for creating processes. Bypasses GIL and efficiently utilizes multiple CPU cores. -
asynciomodule — for asynchronous programming using coroutines. Well-suited for a large number of I/O operations. -
High-level libraries:
concurrent.futures.ThreadPoolExecutorandProcessPoolExecutor— convenient interfaces for running tasks in threads or processes.
Example of using concurrent.futures:
from concurrent.futures import ThreadPoolExecutor
def task(n):
print(f"Task {n} is running")
with ThreadPoolExecutor(max_workers=3) as executor:
for i in range(5):
executor.submit(task, i)
Thus, the choice of method depends on the type of task: for CPU-bound — multiprocessing, for I/O-bound — threading or asyncio.