[Twisted-Python] setTimeout in Deferred

Christopher Armstrong radeex at gmail.com
Tue Mar 8 15:35:54 MST 2005


On Tue, 8 Mar 2005 15:00:50 -0500 (EST), Yun Mao <maoy at cis.upenn.edu> wrote:
> This is the doc of setTimeout:
...
> Any reason why not? It seems to me that setTimeout just calls
> reactor.callLater()

It's a bit more complex than that, and its input and behavior makes
some wrong assumptions and has some bad implications. It is the
end-user of a Deferred who usually wants to specify the timeout before
cancelling the operation. However, it is the creator of the Deferred
who needs to specify _how_ cancelling an operation works -- it's
different for every protocol / kind of operation. Putting the timeout
and the canceller next to each other is a bad idea. In practice, using
setTimeout leads to a bunch of stupid AlreadyCalled errors when the
end-user of a Deferred specifies a timeout and the framework code
doesn't know about it.

The best way to offer timeout support in your code is to modify your
API. If you have a function like getPage which connects to an HTTP
server and downloads a resource, that getPage function should take a
timeout parameter. If set, the HTTP-downloader should disconnect from
the server when the timeout is reached, and simply send an errback to
the deferred.


-- 
  Twisted   |  Christopher Armstrong: International Man of Twistery
   Radix    |    -- http://radix.twistedmatrix.com
            |  Release Manager, Twisted Project
  \\\V///   |    -- http://twistedmatrix.com
   |o O|    |  Founding Member, Hobart Hacking Society
w----v----w-+    -- http://hackingsociety.org/chapters/hash




More information about the Twisted-Python mailing list