Concurrent Processing

Interesting in learning more? Check out Fluent Python

Preliminaries

from concurrent import futures

Create Data

data = range(100)

Create Function

# Create some function that takes a value
def some_function(value):
    # And outputs it raised to its own power
    return value**value

Run The Function On The Data Concurrently

# With a pool of workers
with futures.ProcessPoolExecutor() as executor:
    # Map the function to the data
    result = executor.map(some_function, data)

View Results

# List the first 5 outputs
list(result)[0:5]
[1, 1, 4, 27, 256]