Last active
February 21, 2020 05:13
-
-
Save planetceres/c1663e1f5efe1153c58874bc05c67187 to your computer and use it in GitHub Desktop.
parallel processing with timeout
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
# Source: https://github.com/joblib/joblib/pull/366#issuecomment-267603530 | |
# Source2: https://github.com/joblib/joblib/issues/522 | |
# Source3: https://github.com/mapillary/OpenSfM/blob/199f2dc4a535397afc8fdeb76d67053e76015b61/opensfm/reconstruction.py#L470 | |
import time | |
import multiprocessing | |
import functools | |
from joblib import Parallel, delayed | |
def with_timeout(timeout): | |
def decorator(decorated): | |
@functools.wraps(decorated) | |
def inner(*args, **kwargs): | |
pool = multiprocessing.pool.ThreadPool(1) | |
async_result = pool.apply_async(decorated, args, kwargs) | |
try: | |
return async_result.get(timeout) | |
except multiprocessing.TimeoutError: | |
return | |
return inner | |
return decorator | |
@with_timeout(0.5) | |
def func(to_return, sleep_time): | |
time.sleep(sleep_time) | |
return to_return | |
print(Parallel(n_jobs=2)(delayed(func)(to_return, sleep_time) | |
for to_return, sleep_time in enumerate([ | |
0.1, 0.7, 0.2, 0.3]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment