Junior — Middle
What methods did you use for organizing parallel data processing?
sobes.tech AI
Answer from AI
To organize parallel data processing in Python, I used several approaches:
-
threadingmodule — for thread-level parallelism, useful for I/O-bound tasks. -
multiprocessingmodule — to run multiple processes, which bypasses the GIL and efficiently utilizes multiple CPU cores, especially for CPU-bound tasks. -
concurrent.futureslibrary — provides a convenient interface for running tasks in threads (ThreadPoolExecutor) or processes (ProcessPoolExecutor). -
Asynchronous programming (
asyncio) — for efficient handling of a large number of I/O operations without creating many threads.
Example using concurrent.futures for parallel processing of a list of numbers:
from concurrent.futures import ThreadPoolExecutor
def process_item(item):
return item * item
items = [1, 2, 3, 4, 5]
with ThreadPoolExecutor(max_workers=3) as executor:
results = list(executor.map(process_item, items))
print(results) # [1, 4, 9, 16, 25]