def repeat(f, reps, cpus, **kwargs):
if reps == 1:
f(**kwargs)
return
fname = f.__name__
print("Starting {} {} times with:".format(fname, reps))
print(kwargs)
if cpus == 1:
for _ in range(reps):
try:
f(**kwargs)
except Exception as e:
warnings.warn(str(e))
else:
from multiprocessing import cpu_count
from concurrent.futures import ProcessPoolExecutor, as_completed
if cpus < 1:
cpus = cpu_count()
with ProcessPoolExecutor(cpus) as executor:
futures = [executor.submit(f, **kwargs) for _ in range(reps)]
for fut in as_completed(futures):
if fut.exception():
warnings.warn(str(fut.exception()))
print("Finished")
评论列表
文章目录