Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.internet import defer, reactor
- class TimeoutError(Exception):
- """Raised when time expires in timeout decorator"""
- def timeout(secs):
- """
- Decorator to add timeout to Deferred calls
- """
- def wrap(func):
- @defer.inlineCallbacks
- def _timeout(*args, **kwargs):
- rawD = func(*args, **kwargs)
- if not isinstance(rawD, defer.Deferred):
- defer.returnValue(rawD)
- timeoutD = defer.Deferred()
- timesUp = reactor.callLater(secs, timeoutD.callback, None)
- try:
- rawResult, timeoutResult = yield defer.DeferredList([rawD, timeoutD], fireOnOneCallback=True, fireOnOneErrback=True, consumeErrors=True)
- except defer.FirstError, e:
- #Only rawD should raise an exception
- assert e.index == 0
- timesUp.cancel()
- e.subFailure.raiseException()
- else:
- #Timeout
- if timeoutD.called:
- rawD.cancel()
- raise TimeoutError("%s secs have expired" % secs)
- #No timeout
- timesUp.cancel()
- defer.returnValue(rawResult)
- return _timeout
- return wrap
Add Comment
Please, Sign In to add comment