Re: [Twisted-Python] sleeping in inlineCallbacks
"Brian" == Brian Granger <ellisonbg.net@gmail.com> writes: Brian> I looked at deferLater, but am not using it right now. The sleep Brian> function I am using is basically the same as this - in my case Brian> "sleep" turned out to be a little simpler because I don't have to Brian> create and pass the clock around. But I will probably use Brian> deferLater for this same purpose in the future as well.
Hi Brian Forgive me for butting in, but.... why :-) Importing the reactor is no big deal, and if you use task.deferLater, apart from not having to write any code you also have the advantage of being able to pass it a result that the deferred will be called with. For this reason you can also put a task.deferLater result into a callback chain to achieve an async sleep and it will duly pass the result along. E.g.: from twisted.internet import reactor, task, defer d = somethingReturningADeferred() d.addCallback(lambda x: task.deferLater(reactor, 5.0, defer.passthru, x)) Terry
Hi Brian
Forgive me for butting in, but.... why :-)
Helpful butting in is more than fine..
Importing the reactor is no big deal, and if you use task.deferLater, apart from not having to write any code you also have the advantage of being able to pass it a result that the deferred will be called with.
For this reason you can also put a task.deferLater result into a callback chain to achieve an async sleep and it will duly pass the result along. E.g.:
from twisted.internet import reactor, task, defer
d = somethingReturningADeferred() d.addCallback(lambda x: task.deferLater(reactor, 5.0, defer.passthru, x))
Ahh, I see. I saw the signature of deferLater and the Clock class in task and I thought that I would have to create a Clock instance by hand. That I can simply use the reactor (which of course is already imported and running in my code) simplifies the usage of deferLater. Thanks! But, even with this, I do like the flow and readability of code that uses sleep like this: @inlineCallbacks def f(): result1 = yield somethingDeferred() yield sleep(1.0) result2 = yield anotherDeferred(result2) Rather than the way that deferLater looks: @inlineCallbacks def f(): result1 = yield somethingDeferred() result2 = yield task.deferLater(reactor, 1.0, anotherDeferred, result2) But, the difference at this point is entirely aesthetic. One question though - can someone explain the need/usage cases for task.Clock and the other things in task? Cheers, Brian
Terry
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Thu, Oct 29, 2009 at 8:38 PM, Brian Granger <ellisonbg.net@gmail.com> wrote:
One question though - can someone explain the need/usage cases for task.Clock and the other things in task?
Clock is a "fake" IReactorTime implementation; as the docstring says, it is intended for writing unit tests where you want control over the behaviour of the clock, rather than using the real time source. As for the other things... LoopingCall repeatedly runs a callable at a certain interval, coiterate takes a generator and iterates it cooperatively (returning control back to the reactor between iterations), and cooperate is a new-and-improved version of coiterate which I haven't looked at yet. -- mithrandi, i Ainil en-Balandor, a faer Ambar
On 01:26 am, mithrandi@mithrandi.net wrote:
On Thu, Oct 29, 2009 at 8:38 PM, Brian Granger <ellisonbg.net@gmail.com> wrote:
One question though - can someone explain the need/usage cases for task.Clock and the other things in task?
Clock is a "fake" IReactorTime implementation; as the docstring says, it is intended for writing unit tests where you want control over the behaviour of the clock, rather than using the real time source.
As for the other things... LoopingCall repeatedly runs a callable at a certain interval, coiterate takes a generator and iterates it cooperatively (returning control back to the reactor between iterations), and cooperate is a new-and-improved version of coiterate which I haven't looked at yet.
cooperate and coiterate basically do the same thing. The difference is that cooperate gives you access to extra features, like pausing, resuming, and canceling. Jean-Paul
participants (4)
-
Brian Granger
-
exarkun@twistedmatrix.com
-
Terry Jones
-
Tristan Seligmann