Last active
April 25, 2020 21:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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