[Twisted-Python] sleeping in inlineCallbacks
Brian Granger
ellisonbg.net at gmail.com
Thu Oct 29 12:38:34 MDT 2009
> 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 at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20091029/f0bb8ce6/attachment.html>
More information about the Twisted-Python
mailing list