Skip to content

Instantly share code, notes, and snippets.

@adamloving
Last active June 19, 2018 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/81835cdfe01752abd21dbfde5de42eb4 to your computer and use it in GitHub Desktop.
Save adamloving/81835cdfe01752abd21dbfde5de42eb4 to your computer and use it in GitHub Desktop.
parallelize python method
import multiprocessing, concurrent.futures
from functools import partial
WORKER_THREAD_COUNT = multiprocessing.cpu_count()
def parallelize(partials):
results = []
with concurrent.futures.ProcessPoolExecutor(max_workers=WORKER_THREAD_COUNT) as executor:
jobs = [ executor.submit(p) for p in partials ]
for future in concurrent.futures.as_completed(jobs):
results.append(future.result())
return results
# example
def work(x): print(x)
items = [1,2,3]
partials = map(lambda item: partial(work, item), items)
results = parallelize(partials)
@davepeck
Copy link

👍

@adamloving
Copy link
Author

adamloving commented Jun 19, 2018

Refactored using partials (curried functions) so that parallelize doesn't need to care about function arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment