Sobes.tech
Junior — Middle

What is the mechanism called that allows blocking operations to run in a separate thread or process for parallel processing?

sobes.tech AI

Answer from AI

The mechanism is called asynchronous execution using threads or processes. In Python, modules like threading (for threads) and multiprocessing (for processes) are often used. They allow running blocking operations in separate threads or processes to avoid blocking the main thread and achieve parallel processing.

Example using concurrent.futures.ThreadPoolExecutor:

from concurrent.futures import ThreadPoolExecutor
import time

def blocking_io():
    time.sleep(2)
    return "Done"

with ThreadPoolExecutor() as executor:
    future = executor.submit(blocking_io)
    print(future.result())