Sobes.tech
Junior — Middle

Do you have experience developing multithreaded applications or components that use parallel task execution?

sobes.tech AI

Answer from AI

In Python, the threading module is often used for developing multithreaded applications, allowing multiple threads to run within a single process. However, due to the GIL (Global Interpreter Lock), true parallelism of thread execution is limited, especially for CPU-bound tasks.

For parallel execution of tasks, especially CPU-bound, it is better to use the multiprocessing module, which spawns multiple processes and bypasses the GIL limitation.

For IO-bound tasks, concurrent.futures.ThreadPoolExecutor can be used for convenient thread pool management, and for CPU-bound tasks — concurrent.futures.ProcessPoolExecutor.

Example of using ThreadPoolExecutor for parallel function execution:

from concurrent.futures import ThreadPoolExecutor
import time

def task(n):
    print(f"Task {n} started")
    time.sleep(1)
    print(f"Task {n} completed")

with ThreadPoolExecutor(max_workers=3) as executor:
    for i in range(5):
        executor.submit(task, i)

This code runs up to 3 threads simultaneously, executing the task function in parallel.