Skip to content

Instantly share code, notes, and snippets.

@nitinbhojwani
Created April 8, 2020 12:31
Show Gist options
  • Save nitinbhojwani/cc23d33177351ca5645d686d9381020e to your computer and use it in GitHub Desktop.
Save nitinbhojwani/cc23d33177351ca5645d686d9381020e to your computer and use it in GitHub Desktop.
Using processpool executor in Python3
import concurrent.futures
def test_function(n):
return n ** 2
# initiate a process pool executor
# by default, number of workers = number of CPU cores
executor = concurrent.futures.ProcessPoolExecutor(max_workers=10)
# 1. submit jobs to the executor and track in a list
jobs = []
for i in range(10):
jobs.append(executor.submit(test_function, i))
for future in concurrent.futures.as_completed(jobs):
result = future.result()
print(result)
# do whatever you want to do here:
# if the exception is thrown from the child fn catch here
# 2. if we want to track the input or other identifier with the result,
# track the submitted jobs to the executor using a dict
jobs = {}
for id in range(10):
jobs[executor.submit(test_function, id)] = id
for future in concurrent.futures.as_completed(jobs):
id = jobs[future] # this reflects id above
result = future.result()
print(id, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment