I recently had to deal with twisted code, everything was already set up since I was writing a plugin. I never delt with asynchronous programming or twisted before, so my first problem was understanding what all those addCallback where doing.
First of all addCallback is a method of defer.Deferred
objects that expect as argument a callable (a function). A must to read is the official Deferred Reference. As introduction, a Deffered is an object that “hold” the result until is ready and when it will be the added callback function will be called with that result as argument, this function will return a new result that will be hold for a while and when it will be available it will be passed to the next added callback and so on. This is a chain of callbacks.
The consequences are that you’ll have to think in a more functional way and you’ll baiscally have two kinds of functions, the “twisted aware” ones and the normal ones. Here’s an example that might help explain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from twisted.internet import reactor, defer
def double(x):
# usually here there's something expensive
# that's going to take some time
return x*x
def print_triple(x):
# we need to think in a functional way, so we need a function
# to print a result
print 'the triple of {} is {}'.format(x, 3*x)
def main():
d = defer.Deferred()
d.addCallback(double)
d.addCallback(print_triple)
# let's start the callback chain, inserting the first value
d.callback(10)
main()
# manually set up the end of the process by asking the reactor to
# stop itself in 1 seconds time
reactor.callLater(1, reactor.stop)
# start up the Twisted reactor (event loop handler) manually
reactor.run()
Those reactor run and (dalyed) stop are just to start and stop the twisted engine. The main point here is that one has to think functionally for everything, because all the values/results are available only to functions through the callback system.
Comments powered by Disqus.