[Twisted-Python] sleeping in inlineCallbacks
Is there any way to do something like "yield sleep(10)" inside an @inlineCallbacks method?
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. On Wed, Oct 7, 2009 at 3:35 PM, Paul Thomas <spongelavapaul@googlemail.com>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?
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
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. -phil
On 10:04 pm, phil@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
On Wed, Oct 7, 2009 at 5:29 PM, <exarkun@twistedmatrix.com> wrote:
Recent versions of Twisted include twisted.internet.defer.deferLater, a function rather similar to the sleep function defined above.
Jean-Paul
Do you mean twisted.internet.task.deferLater? Or am I missing something? Kevin Horn
On 7 Oct, 10:40 pm, kevin.horn@gmail.com wrote:
On Wed, Oct 7, 2009 at 5:29 PM, <exarkun@twistedmatrix.com> wrote:
Recent versions of Twisted include twisted.internet.defer.deferLater, a function rather similar to the sleep function defined above.
Jean-Paul Do you mean twisted.internet.task.deferLater? Or am I missing something?
Nope, you're right. Thanks for the correction. Jean-Paul
On Wed, Oct 7, 2009 at 6:04 PM, Phil Christensen <phil@bubblehouse.org>wrote:
Paul's example will work just fine. Also, AFAIK there's no existing twisted function to do replicate an asynchronous sleep() in this way.
-phil
What about Deferred.setTimeout? http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr... IMHO it solves similar problem. -- Mikhail Terekhov
On Wed, Oct 7, 2009 at 11:08 PM, Mikhail Terekhov <termim@gmail.com> wrote:
What about Deferred.setTimeout? http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr... IMHO it solves similar problem.
Deferred.setTimeout does something else. More importantly, it is deprecated. Furthermore, it's fundamentally broken and cannot be made to work in a reliable way. Don't use it, or your Twisted programs will break in a future release of Twisted Core.
Mikhail Terekhov wrote:
On Wed, Oct 7, 2009 at 6:04 PM, Phil Christensen <[1]phil@bubblehouse.org> wrote:
Paul's example will work just fine. Also, AFAIK there's no existing twisted function to do replicate an asynchronous sleep() in this way.
-phil
What about Deferred.setTimeout? [2]http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr... IMHO it solves similar problem.
Deferred.setTimeout is a poor API and is deprecated (despite what the automatically generated API docs think). <http://twistedmatrix.com/trac/ticket/178> has some discussion (and links to more discussion) explaining what's wrong with it, and what a good replacement would look like. So please, don't use setTimeout, and definitely do not recommend it to others! -Andrew.
I have been using Twisted for years but only recently began to use inlineCallbacks (couldn't give up Python 2.4 support until recently). This simple asynch. sleep function is fantastic. Just today I used to it with inlineCallbacks to dramatically clean up some complex deferred logic. Thanks, Brian On Wed, Oct 7, 2009 at 8:28 PM, Andrew Bennetts <andrew@bemusement.org>wrote:
On Wed, Oct 7, 2009 at 6:04 PM, Phil Christensen <[1]
Mikhail Terekhov wrote: phil@bubblehouse.org>
wrote:
Paul's example will work just fine. Also, AFAIK there's no existing twisted function to do replicate an asynchronous sleep() in this way.
-phil
What about Deferred.setTimeout? [2] http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr... IMHO it solves similar problem.
Deferred.setTimeout is a poor API and is deprecated (despite what the automatically generated API docs think). <http://twistedmatrix.com/trac/ticket/178> has some discussion (and links to more discussion) explaining what's wrong with it, and what a good replacement would look like.
So please, don't use setTimeout, and definitely do not recommend it to others!
-Andrew.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Hi Brian, In general, please reply on this list inline, with quoting and trimming, like so: http://en.wikipedia.org/wiki/Posting_style#Inline_replying This might seem like a minor thing, but it really helps those of us who have to follow long discussions and many mailing lists. Now, on to the main point of your message: On Oct 28, 2009, at 5:48 PM, Brian Granger wrote:
I have been using Twisted for years but only recently began to use inlineCallbacks (couldn't give up Python 2.4 support until recently). This simple asynch. sleep function is fantastic. Just today I used to it with inlineCallbacks to dramatically clean up some complex deferred logic.
Which asynch sleep function? I hope you're not talking about this stuff, quoted in your message:
On Wed, Oct 7, 2009 at 8:28 PM, Andrew Bennetts <andrew@bemusement.org> wrote:
What about Deferred.setTimeout?
Please note spiv's reply:
Deferred.setTimeout is a poor API and is deprecated (despite what the automatically generated API docs think).
and his recommendation:
So please, don't use setTimeout, and definitely do not recommend it to others!
I hope that, instead, you're talking about the deferLater function that was introduced in Twisted 8.0? http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.task.html#defe... This will act like an async 'sleep' if its result is yielded from an @inlineCallbacks function.
Glyph, In general, please reply on this list inline, with quoting and trimming,
like so:
http://en.wikipedia.org/wiki/Posting_style#Inline_replying
This might seem like a minor thing, but it really helps those of us who have to follow long discussions and many mailing lists.
Sorry, just being lazy... I have been using Twisted for years but only recently began to use
inlineCallbacks (couldn't give up Python 2.4 support until recently). This simple asynch. sleep function is fantastic. Just today I used to it with inlineCallbacks to dramatically clean up some complex deferred logic.
Which asynch sleep function?
This one posted by Paul: def sleep(seconds): d = defer.Deferred() reactor.callLater(seconds, d.callback, seconds) return d I hope you're not talking about this stuff, quoted in your message:
On Wed, Oct 7, 2009 at 8:28 PM, Andrew Bennetts <andrew@bemusement.org>wrote:
What about Deferred.setTimeout?
Please note spiv's reply:
Deferred.setTimeout is a poor API and is deprecated (despite what the
automatically generated API docs think).
and his recommendation:
So please, don't use setTimeout, and definitely do not recommend it to
others!
Definitely not - I did see this warning.
I hope that, instead, you're talking about the deferLater function that was introduced in Twisted 8.0?
http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.task.html#defe...
This will act like an async 'sleep' if its result is yielded from an @inlineCallbacks function.
I looked at deferLater, but am not using it right now. The sleep function I am using is basically the same as this - in my case "sleep" turned out to be a little simpler because I don't have to create and pass the clock around. But I will probably use deferLater for this same purpose in the future as well. Cheers, Brian
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (9)
-
Andrew Bennetts
-
Brian Granger
-
exarkun@twistedmatrix.com
-
Glyph Lefkowitz
-
Kevin Horn
-
Matt Perry
-
Mikhail Terekhov
-
Paul Thomas
-
Phil Christensen