Created
March 7, 2019 21:09
-
-
Save bkj/672f31ef0d90231799136d261b2659a9 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
timed_parmap.py | |
""" | |
import sys | |
from joblib import delayed | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
def __delayed_runner(job): | |
return job[0](*job[1], **job[2]) | |
def timed_parmap(jobs, n_jobs=2, timeout=1): | |
with ProcessPoolExecutor(max_workers=n_jobs) as runner: | |
jobs = {runner.submit(__delayed_runner, job) for job in jobs} | |
try: | |
for job in as_completed(jobs, timeout=timeout): | |
yield job.result() | |
except: | |
print('timed_parallel: timeout at %d' % timeout, file=sys.stderr) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment