[Twisted-Python] Timeouts for Deferreds?

Andrew Bennetts andrew-twisted at puzzling.org
Thu May 2 21:00:11 MDT 2002


I just thought of another feature I would find handy in Deferreds,
although perhaps this feature belongs somewhere else...

Deferred.timeout(secs):
    If I am uncalled for secs seconds after being armed, call my errback
    automatically.

This way callers can control how long they will wait for a Deferred to
do something.  Methods returns Deferreds could also set a reasonable
default with .timeout that the caller can override by calling .timeout
again.  A timeout of None would, of course, mean never timeout.

I can see this as being useful in many ways.  The biggest problem with
it is I'm not sure what should happen when you try to call a Deferred
after it has timed out (e.g. your CGI script took 5 minutes, timed out
and thus called errback, but did eventually finish).  I don't think
calling callback/errback should raise an AlreadyCalledException, because
the timeout may have been set by a client of a library that doesn't
expect timeouts.  On the other hand, I don't like the idea of things
failing silently.  The ideal situation is, of course, to make sure that
the timeout event always cancels whatever it is that would later call
callback/errback, but I don't like making it a requirement that this
must happen (plus it probably means adding a "timeback" method, or
something).

Perhaps a TimeoutDeferred subclass?  But then it's not generally useful
anymore, unless a method explicitly returns TimeoutDeferred and not
Deferred.  Still, encouraging people to use hard-coded arbitrary
timeouts everywhere probably isn't a good idea either.

Thoughts, anyone?

(Actual implementation details left as an exercise for the reader...
actually, I'm happy to do it if people want this).

-Andrew.





More information about the Twisted-Python mailing list