Created
February 29, 2016 15:43
-
-
Save tarekziade/365099a010589aadb405 to your computer and use it in GitHub Desktop.
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
import os | |
from multiprocessing.pool import Pool, TimeoutError | |
import signal | |
import functools | |
import time | |
class TimeoutFunc(object): | |
def __init__(self, func, duration, ctimeout): | |
self.func = func | |
self.duration = duration | |
self.ctimeout = ctimeout | |
def handler(self, signum, frame): | |
self.ctimeout() | |
raise TimeoutError | |
def __call__(self, *args, **kw): | |
old_handler = signal.signal(signal.SIGALRM, self.handler) | |
signal.alarm(self.duration) | |
try: | |
return self.func(*args, **kw) | |
finally: | |
signal.signal(signal.SIGALRM, old_handler) | |
signal.alarm(0) | |
class TimeoutPool(Pool): | |
def async_with_timeout(self, func, args, callback, duration, ctimeout): | |
wrapped = TimeoutFunc(func, duration, ctimeout) | |
return self.apply_async(wrapped, args, callback=callback) | |
def f(x): | |
print('start %d' % os.getpid()) | |
try: | |
time.sleep(1.1) | |
return x*x | |
finally: | |
print('end %d' % os.getpid()) | |
def ok(result): | |
print(result) | |
def timedout(): | |
print("timeout!") | |
pool = TimeoutPool(processes=2) | |
pool.async_with_timeout(f, (10,), callback=ok, duration=10, | |
ctimeout=timedout) | |
pool.async_with_timeout(f, (2,), callback=ok, duration=1, | |
ctimeout=timedout) | |
pool.close() | |
pool.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment