Middle — Middle+
How is parallel execution of tasks achieved using multiprocessing?
sobes.tech AI
Answer from AI
In Python, concurrency can be organized using several methods:
-
Threads (threading) — suitable for I/O-bound tasks, such as network or file operations. I used
threading.Threadto run multiple tasks simultaneously. -
Processes (multiprocessing) — for CPU-bound tasks, to bypass GIL limitations. Used the
multiprocessingmodule for parallel data processing or computations. -
Async programming (asyncio) — for managing many I/O operations efficiently without creating many threads. Used
async defandawaitfor non-blocking code. -
Locks and synchronization — used
threading.Lock,RLockto protect shared resources.
Example with threads:
import threading
def worker():
print("Work in thread")
threads = []
for i in range(5):
t = threading.Thread(target=worker)
t.start()
threads.append(t)
for t in threads:
t.join()
Thus, the choice depends on the task: I/O-bound — threads or asyncio, CPU-bound — processes.