class DeferredWrapperWithTimeout(object):
    """
    Holds a deferred that allows a specified function to be called-back
    if the deferred does not fire before some specified timeout.
    """

def __init__(self, canceller=None):
    self._def = defer.Deferred(canceller)

def _finish(self, r, t):
    """
        Function to be called (internally) after the Deferred
        has fired, in order to cancel the timeout.
        """
if t != None and t.active():
    t.cancel()
return r

def getDeferred(self):
    return self._def

def addTimeoutCallback(self, reactr, timeout, callUponTimeout, *args, **kw):
    """
        The function 'callUponTimeout' (with optional args or keywords)
        will be called after 'timeout' seconds, unless the Deferred fires.
        """

def timeoutCallback():
    self._def.cancel()
callUponTimeout(*args, **kw)
toc = reactr.callLater(timeout, timeoutCallback)
return self._def.addCallback(self._finish, toc)
Как добавить таймаут к скрученной отсрочке