[Twisted-Python] deferToThread - supported alternative to the (deprecated) setTimeout method
L. Daniel Burr
ldanielburr at mac.com
Tue May 23 16:39:43 MDT 2006
Hi Paul,
On Tue, 23 May 2006 15:49:23 -0500, Paul Moore <pf_moore at yahoo.co.uk>
wrote:
> I have a program where I think that what I want to use is
> deferToThread and setTimeout. However, the setTimeout method is marked
> as deprecated - but I can't find a good supported alternative. Can
> anyone help?
>
I don't know for a certainty that I can help, but I'll try :)
> The situation I have is:
>
> I need to do a large-ish number (50-100) of blocking calls in
> parallel, and collect the results or any errors. In itself, this seems
> like a suitable use for deferToThread (defer each call, and collect
> the results in the deferred callback/errback).
>
Agreed, this seems to be a perfectly reasonable use-case for deferToThread.
> The problem is that in rare cases, the blocking call can block
> indefinitely. In this case, I need to make the call time out. However,
> the underlying API offers no way to time out the call, so I have to do
> this externally.
>
Alas, when dealing with APIs that do not allow for timeouts, there is very
little that twisted can do to help. The use of setTimeout has been
debated to death, and no magic bullet solution is in sight. That said,
please read the following discussion thread to see what your options are,
and perhaps one of them will be acceptable for your particular situation.
These discussions cover your issue in reasonable depth.
http://twistedmatrix.com/pipermail/twisted-python/2004-April/007531.html
http://twistedmatrix.com/trac/ticket/178
http://twistedmatrix.com/pipermail/twisted-python/2005-March/009716.html
Basically, the best you can do in your circumstances is use
reactor.callLater to invoke some function/method that will take action
appropriate to your situation, be that cancelling the Deferred, killing
the thread, etc.
Hope this helps,
L. Daniel Burr
More information about the Twisted-Python
mailing list