Python Twisted’s inlineCallBacks

The folks in the twisted community found the most wonderful trick.  Consider this typical example of event driven code.  Consider this example (proabably easier to read bottom to top).  The function enqueueTaskToDisplayURL depends on three callbacks to handle the usual eventualities (good, bad, wrapup).

from twisted.web import client
from twisted.internet import reactor
import sys

def shutdown(*whatever): reactor.stop()

def printPage(data): print data

def printError(failure):
    print >> sys.stderr, "Error:", failure.getErrorMessage()

def enqueueTaskToDisplayURL(url):
    get = client.getPage(url)

 if __name__ == "__main__":

The trick they found was to use generators of their promise abstraction (aka defer).  So we can write that example as so:

from twisted.internet import defer
from twisted.web import client
from twisted.internet import reactor
import sys

def enqueueTaskToDisplayURL(url):
        data = yield client.getPage(url)
        print data
    except Exception, e:
        print >> sys.stderr, "Error:", e
    finally: reactor.stop()

if __name__ == "__main__":

The clever hack is done by @defer.inlineCallbacks, which works because this function (notice the yield) is a generator.  That just made be giggle.

I’ve only gotten into Python and Twisted in a part time way over the past few months.  I’m probably committing all kinds of sins here.

2 thoughts on “Python Twisted’s inlineCallBacks

  1. Alex Clemesha

    Nice article. Last month I read another article you wrote, – which also was cool – tying together AMQP and syslog, etc. Anyways, I wanted to point out that there is a great Twisted AMQP client lib called txAMQP ( by Esteve Fernandez that might catch your interest. The examples and tests utilize inlineCallbacks all over the place, in very useful and clean ways. Check the source code.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>