[Twisted-Python] sleeping in inlineCallbacks

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Oct 7 18:29:11 EDT 2009


On 10:04 pm, phil at bubblehouse.org wrote:
>On Oct 7, 2009, at 3:46 PM, Matt Perry wrote:
>>On 7 Oct 2009, at 20:30, Paul Thomas wrote:
>
>>>Is there any way to do something like "yield sleep(10)" inside an
>>>@inlineCallbacks method?
>>>
>>>I realise I can do this:
>>>
>>>def sleep(seconds):
>>>     d = defer.Deferred()
>>>     reactor.callLater(seconds, d.callback, seconds)
>>>     return d
>>>
>>>but I wondered if something like that is already in the libraries?
>>
>
>>reactor.callLater() is the best way to delay a certain function
>>call.  sleep() will block the entire program from running, which you
>>probably don't want to do.
>
>Not in this case; note he's referring to his custom sleep() function,
>not time.sleep()
>
>Paul's example will work just fine. Also, AFAIK there's no existing
>twisted function to do replicate an asynchronous sleep() in this way.

Recent versions of Twisted include twisted.internet.defer.deferLater, a 
function rather similar to the sleep function defined above.

Jean-Paul



More information about the Twisted-Python mailing list