Last active
June 1, 2020 23:26
-
-
Save JoaoCarabetta/a1ce30916bd857d447958f9191406858 to your computer and use it in GitHub Desktop.
Ready to go parallel process implementation for functions with more than one argument
This file contains hidden or 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.pool import Pool | |
from functools import partial | |
import time | |
n_processes = 3 | |
def func(extra, t): | |
time.sleep(t + extra) | |
print('t', t, 'extra', extra) | |
def func_generic(tupl): | |
print('t', tupl[0], 'extra', tupl[1]) | |
# Fixed Extra Variable | |
times = [1,2,3] | |
extra = 1 | |
with Pool(n_processes) as p: | |
p.map(partial(func, extra), times) | |
# Multiple Extra Variables | |
times = [1,2,3] | |
extras = [4,5,6] | |
with Pool(n_processes) as p: | |
p.map(partial(func_generic), zip(times, extras)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Na real, precisa do
partial
sim pq olambda
não é um objeto que suporta pickle