[Twisted-Python] async code structure question
Mary Gardiner
mary-twisted at puzzling.org
Wed Feb 9 16:52:15 MST 2005
On Wed, Feb 09, 2005, snacktime wrote:
> Say I have a class with several functions that are called in order.
> Each function relies on the results of the previous function in order
> to proceed. To write this asynchronously, do I chain them together so
> that each function is called as a deferred, with the callback pointing
> to the next function and so forth down the list?
It depends on whether these are synchronous or asynchonrous functions.
For synchronous, let's assume you call one function that returns a
Deferred, and then the rest of them manipulate the results of that
function. You just add all the manipulating functions as callbacks:
from twisted.internet import defer
def getDummyDeferred():
"""
Dummy method which returns a Deferred for us to play with
Usually this would be some library function that does something async
"""
# defer.succeed returns a Deferred fired with its argument
return defer.succeed(3)
def multiplyByTwo(result):
return result * 2
def square(result):
return result * result
def printResult(result):
print "result is %d" % result
def main():
d = getDummyDeferred()
d.addCallback(multiplyByTwo)
d.addCallback(square)
d.addCallback(printResult)
if __name__ == '__main__':
main()
For asynchronous, you chain the Deferreds:
from twisted.internet import defer
def getDummyDeferred():
return defer.succeed(2)
def getDummyDeferredTwo(result):
return defer.succeed(result * 3)
def printResult(result):
print result
def main():
d = getDummyDeferred()
d.addCallback(getDummyDeferredTwo)
d.addCallback(printResult)
You could have some fun converting these all to use reactor.callLater in
the getDummyDeferred and getDummyDeferredTwo methods rather than using
defer.succeed to generate an already fired Deferred :) (This will mean
using reactor.run() in main.)
Relevant examples are at
http://twistedmatrix.com/documents/current/howto/defer#auto5
For (a tiny bit) more on chaining Deferreds together, have a look at
http://twistedmatrix.com/documents/current/howto/defer#auto12. There's
already a bug filed about improving this bit:
http://twistedmatrix.com/bugs/issue596
Comments welcome.
-Mary
More information about the Twisted-Python
mailing list