[Twisted-Python] deferToThread - supported alternative to the (deprecated) setTimeout method

Eric Mangold teratorn at twistedmatrix.com
Tue May 23 19:03:29 EDT 2006


On Wed, 24 May 2006 06:49:23 +1000, 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?

You can just use reactor.callLater(...) to run whatever timeout code you
have after the given number of seconds. callLater returns a DelayedCall
instance which you can .cancel() if you get a result before the timeout.

> 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).
>
> 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.

Well this is a bit of a problem. You can't just kill a thread - so it will
eventually return a result, and Twisted will .callback() its deferred. If
you've already stepped in, due to a timeout, and .callback()'ed the same
deferred you'll get an AlreadyCalledError in your log.

So don't do that.. don't fire that deferred. When you time out just keep  
some
state so that you can ignore the result if and when it comes.

> A quick prototype seems to work OK, using setTimeout to force a
> timeout on the deferred, but it generates deprecation warnings for the
> setTimeout call.
>
> What should I be using to achieve this effect?
>
> Thanks,
> Paul.

Hope that helps.

-- 
Eric Mangold - Twisted/Win32 Maintainer
http://twistedmatrix.com/trac/wiki/Windows




More information about the Twisted-Python mailing list