parallel/serial execute accepts several arguments now

This commit is contained in:
NichtJens
2020-11-02 14:43:20 +01:00
parent f0fda94766
commit 84d7e4882a
2 changed files with 9 additions and 8 deletions

View File

@ -1,20 +1,21 @@
from concurrent.futures import ThreadPoolExecutor
def parallel(func, targets, names=None):
def parallel(func, *targets, names=None):
with ThreadPoolExecutor() as executor:
results = executor.map(func, targets)
if names:
results = executor.map(func, *targets)
if names is not None:
return dict(zip(names, results))
else:
return list(results)
def serial(func, targets, names=None):
if names:
return {n: func(t) for n, t in zip(names, targets)}
def serial(func, *targets, names=None):
targets = zip(*targets)
if names is not None:
return {n: func(*t) for n, t in zip(names, targets)}
else:
return [func(t) for t in targets]
return [func(*t) for t in targets]