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

Jean-Paul Calderone exarkun at divmod.com
Tue May 23 20:43:35 MDT 2006


On Tue, 23 May 2006 21:49:23 +0100, 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?
>
>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.
>
>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?

There's some code along these lines here:

http://twistedmatrix.com/trac/browser/trunk/twisted/internet/base.py#L201

It might make a good example to work from.  Note that any call that
blocks indefinitely will hold on to a thread indefinitely, and thus
reduce your effective thread pool size by one.  If this happens
enough, you'll end up with no free threads in your threadpool, and
no other threaded tasks will ever be able to complete.

Jean-Paul




More information about the Twisted-Python mailing list