Junior — Middle
Have you had experience with multithreading in Python and how did you implement parallel task execution?
sobes.tech AI
Answer from AI
In Python, the modules threading and multiprocessing are often used to implement parallel task execution.
threadingis suitable for I/O-bound tasks, as threads do not execute simultaneously on multiple cores due to the GIL (Global Interpreter Lock), especially for CPU-intensive tasks.multiprocessingcreates separate processes, bypassing the GIL limitation, and is suitable for CPU-intensive tasks.
Example using threading for parallel function execution:
import threading
def task(name):
print(f"Task {name} started")
# simulate work
import time
time.sleep(2)
print(f"Task {name} completed")
threads = []
for i in range(3):
t = threading.Thread(target=task, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
For CPU-intensive tasks, it is better to use multiprocessing:
from multiprocessing import Process
def task(name):
print(f"Process {name} started")
# heavy computation
result = sum(i*i for i in range(10**6))
print(f"Process {name} finished with result {result}")
processes = []
for i in range(3):
p = Process(target=task, args=(i,))
processes.append(p)
p.start()
for p in processes:
p.join()
Thus, the choice of approach depends on the task type: I/O or CPU.