[Twisted-Python] Timeout for Deferred objects

Jean-Paul Calderone exarkun at divmod.com
Sat Dec 8 11:25:40 EST 2007

On Sat, 8 Dec 2007 11:52:00 +0200, Mustafa Sakalsiz <sakalsiz at gmail.com> wrote:
>I want set a timeout for Deferred objects. There is a method in
>Deferred class, setTimeout. It works well for me, but it gives a
>deprecation warning. If setTimeout is deprecated, what else I should
>use for timeouts.

It's deprecated because having a timeout for a Deferred doesn't make sense.
A Deferred is just a convenient API for associating a result with the code
which is interested in the result.  It doesn't ever produce the result itself,
so it isn't the right object to give a timeout to.

Instead, the code which is responsible for calling back the Deferred is the
code which needs to be given a timeout.  There's no general way to do this,
since the code for calling back Deferreds is different for each Deferred.
Some APIs support timeouts already - for example, reactor.resolve - but
not all of them do.

You can add a timeout to an API which doesn't support timeouts in one of
two ways:

  - Add it and implement whatever changes are necessary so that after the
    timeout the underlying operation is cancelled.  You may want to submit
    patches. :)

  - Wrap the original API with one which supports a timeout.  This will
    return a Deferred it creates and call it back when the underlying
    Deferred is called back - unless the timeout has expired, then it will
    just drop the result.

In either case, you'll probably use reactor.callLater to implement the


More information about the Twisted-Python mailing list