synapse/util/async.py
def add_timeout_to_deferred(deferred, timeout, on_timeout_cancel=None):
    """
    Add a timeout to a deferred by scheduling it to be cancelled after
    timeout seconds.

    This is essentially a backport of deferred.addTimeout, which was introduced
    in twisted 16.5.

    If the deferred gets timed out, it errbacks with a DeferredTimeoutError,
    unless a cancelable function was passed to its initialization or unless
    a different on_timeout_cancel callable is provided.

    Args:
        deferred (defer.Deferred): deferred to be timed out
        timeout (Number): seconds to time out after

        on_timeout_cancel (callable): A callable which is called immediately
            after the deferred times out, and not if this deferred is
            otherwise cancelled before the timeout.

            It takes an arbitrary value, which is the value of the deferred at
            that exact point in time (probably a CancelledError Failure), and
            the timeout.

            The default callable (if none is provided) will translate a
            CancelledError Failure into a DeferredTimeoutError.
    """
    timed_out = [False]

    def time_it_out():
        timed_out[0] = True
        deferred.cancel()

    delayed_call = reactor.callLater(timeout, time_it_out)

    def convert_cancelled(value):
        if timed_out[0]:
            to_call = on_timeout_cancel or _cancelled_to_timed_out_error
            return to_call(value, timeout)
        return value

    deferred.addBoth(convert_cancelled)

    def cancel_timeout(result):
        # stop the pending call to cancel the deferred if it's been fired
        if delayed_call.active():
            delayed_call.cancel()
        return result

    deferred.addBoth(cancel_timeout)

    Add a timeout to a deferred by scheduling it to be cancelled after
    timeout seconds.

    This is essentially a backport of deferred.addTimeout, which was introduced
    in twisted 16.5.

    If the deferred gets timed out, it errbacks with a DeferredTimeoutError,
    unless a cancelable function was passed to its initialization or unless
    a different on_timeout_cancel callable is provided.

    Args:
        deferred (defer.Deferred): deferred to be timed out
        timeout (Number): seconds to time out after

        on_timeout_cancel (callable): A callable which is called immediately
            after the deferred times out, and not if this deferred is
            otherwise cancelled before the timeout.

            It takes an arbitrary value, which is the value of the deferred at
            that exact point in time (probably a CancelledError Failure), and
            the timeout.

            The default callable (if none is provided) will translate a
            CancelledError Failure into a DeferredTimeoutError.
    
Add a timeout to a deferred by scheduling it to be cancelled after
timeout seconds.

This is essentially a backport of deferred.addTimeout, which was introduced
in twisted 16.5.

If the deferred gets timed out, it errbacks with a DeferredTimeoutError,
unless a cancelable function was passed to its initialization or unless
a different on_timeout_cancel callable is provided.
Add a timeout to a deferred by scheduling it to be cancelled after timeout seconds . This is essentially a backport of deferred . addTimeout which was introduced in twisted 16 . 5 . If the deferred gets timed out it errbacks with a DeferredTimeoutError unless a cancelable function was passed to its initialization or unless a different on_timeout_cancel callable is provided .
def add_timeout_to_deferred(deferred, timeout, on_timeout_cancel=None):
    timed_out = [False]
    def time_it_out():
        timed_out[0] = True
        deferred.cancel()
    delayed_call = reactor.callLater(timeout, time_it_out)
    def convert_cancelled(value):
        if timed_out[0]:
            to_call = on_timeout_cancel or _cancelled_to_timed_out_error
            return to_call(value, timeout)
        return value
    deferred.addBoth(convert_cancelled)
    def cancel_timeout(result):
        if delayed_call.active():
            delayed_call.cancel()
        return result
    deferred.addBoth(cancel_timeout)
def add_timeout_to_deferred ( deferred , timeout , on_timeout_cancel = None ) : timed_out = [ False ] def time_it_out ( ) : timed_out [ 0 ] = True deferred . cancel ( ) delayed_call = reactor . callLater ( timeout , time_it_out ) def convert_cancelled ( value ) : if timed_out [ 0 ] : to_call = on_timeout_cancel or _cancelled_to_timed_out_error return to_call ( value , timeout ) return value deferred . addBoth ( convert_cancelled ) def cancel_timeout ( result ) : if delayed_call . active ( ) : delayed_call . cancel ( ) return result deferred . addBoth ( cancel_timeout )
Add a timeout to a deferred by scheduling it to be cancelled after
timeout seconds.
Add a timeout to a deferred by scheduling it to be cancelled after timeout seconds .
"""
    Add a timeout to a deferred by scheduling it to be cancelled after
    timeout seconds.

    This is essentially a backport of deferred.addTimeout, which was introduced
    in twisted 16.5.

    If the deferred gets timed out, it errbacks with a DeferredTimeoutError,
    unless a cancelable function was passed to its initialization or unless
    a different on_timeout_cancel callable is provided.

    Args:
        deferred (defer.Deferred): deferred to be timed out
        timeout (Number): seconds to time out after

        on_timeout_cancel (callable): A callable which is called immediately
            after the deferred times out, and not if this deferred is
            otherwise cancelled before the timeout.

            It takes an arbitrary value, which is the value of the deferred at
            that exact point in time (probably a CancelledError Failure), and
            the timeout.

            The default callable (if none is provided) will translate a
            CancelledError Failure into a DeferredTimeoutError.
    """ # stop the pending call to cancel the deferred if it's been fired
deferred
deferred to be timed out
deferred to be timed out
timeout
seconds to time out after
seconds to time out after
on_timeout_cancel
A callable which is called immediately
after the deferred times out, and not if this deferred is
otherwise cancelled before the timeout.

It takes an arbitrary value, which is the value of the deferred at
that exact point in time (probably a CancelledError Failure), and
the timeout.

The default callable (if none is provided) will translate a
CancelledError Failure into a DeferredTimeoutError.
A callable which is called immediately after the deferred times out and not if this deferred is otherwise cancelled before the timeout . It takes an arbitrary value which is the value of the deferred at that exact point in time ( probably a CancelledError Failure ) and the timeout . The default callable ( if none is provided ) will translate a CancelledError Failure into a DeferredTimeoutError .