Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Last active April 25, 2020 21:31
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 h2rashee/6684527a10f1b186b4eba603d99ee752 to your computer and use it in GitHub Desktop.
Save h2rashee/6684527a10f1b186b4eba603d99ee752 to your computer and use it in GitHub Desktop.
Given a list of values, a function and the amount of concurrency to work with, apply the function so that it processes the function at the same time instead of blocking
from multiprocessing import Pool
def fn(x):
return x+1
def solution(vals, concurrency=1):
if concurrency < 1:
raise Exception('Concurrency value invalid')
with Pool(concurrency) as p:
return p.map(fn, vals)
print(solution([1, 2, 3], 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment