The concurrent module in Python facilitates concurrent programming, particularly for task scheduling and managing thread/process pools. Its core component is concurrent.futures, which offers high-level interfaces for handling threads and processes.
Key Methods
submit(fn, *args, **kwargs): Asynchronously submits a task and returns aFutureobjectmap(fn, iterable): Similar to built-inmap(), but executes concurrentlyshutdown(wait=True): Closes the thread/process pool;wait=Trueensures all tasks complete before shutdownresult(): Retrieves the result from aFuture(blocks until completion)done(): Checks if a task has finishedadd_done_callback(fn): Automatically invokes a function when a task completesas_completed(): Returns results in the order tasks finishwait(): Waits for all tasks to complete with configurable conditions (all or any)
The wait() function returns a named tuple containing:
done: A set of comlpetedFutureobjectsnot_done: A set of pendingFutureobjects
Note that you should iterate over the done set rather than dircetly iterating over the result of wait(futures).
Recommended Usage Patterns
ThreadPoolExecutor: Ideal for I/O-intensive operations like network requests and file operationsProcessPoolExecutor: Suitable for CPU-intensive tasks such as image processing and scientific computations
def worker_task(n): print(f"Starting work on {n}") time.sleep(5) print(f"Completed work on {n}") return n * n
def cpu_intensive_task(n): print("Initiating process ... ", n) time.sleep(5) print("Process concluded ... ", n) return n * n
if name == 'main': # Example with ThreadPoolExecutor # with ThreadPoolExecutor(max_workers=3) as executor: # results = executor.map(worker_task, [1, 2, 3, 4, 5]) # print(list(results))
# Example with ProcessPoolExecutor
with ProcessPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(cpu_intensive_task, i) for i in range(5)]
# Option 1: Process results as they complete
# for future in as_completed(futures):
# print(future.result())
# Option 2: Wait for all and process completed ones
done_futures, pending_futures = wait(futures)
for future in done_futures:
print(future.result())
</div>