Concurrency in Python: Threading and Multiprocessing

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 a Future object
  • map(fn, iterable): Similar to built-in map(), but executes concurrently
  • shutdown(wait=True): Closes the thread/process pool; wait=True ensures all tasks complete before shutdown
  • result(): Retrieves the result from a Future (blocks until completion)
  • done(): Checks if a task has finished
  • add_done_callback(fn): Automatically invokes a function when a task completes
  • as_completed(): Returns results in the order tasks finish
  • wait(): Waits for all tasks to complete with configurable conditions (all or any)

The wait() function returns a named tuple containing:

  • done: A set of comlpeted Future objects
  • not_done: A set of pending Future objects

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 operations
  • ProcessPoolExecutor: 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>

Tags: python Concurrency multithreading multiprocessing ThreadPool

Posted on Sun, 17 May 2026 21:35:19 +0000 by allinurl