A Quick Jump Into Twisted

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:

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()
    # let's start the callback chain, inserting the first value


# 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

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.