[Twisted-Python] inlineCallbacks: Twisted 2.5's generator-based Deferred microthreads-thingie

Itamar Shtull-Trauring itamar at itamarst.org
Fri Feb 16 17:23:02 EST 2007

Apparently some people are not aware of this new feature in Twisted 2.5
(requires Python 2.5), written by James Knight based on code by Chris
Armstrong. Previous versions of Twisted have deferredGenerator, but the
syntax isn't as nice.

Here's some sample code JP Calderone posted to python-dev recently:

    from twisted.internet import reactor
    from twisted.internet.defer import inlineCallbacks
    from twisted.web.client import getPage

    def fetchSequence(...):
        homepage = yield getPage(homepage)
        firstData = yield getPage(anotherPage)
        if someCondition(firstData):
                secondData = yield getPage(wherever)
                if someOtherCondition(secondData):

Essentially this allows one to "wait" for a Deferred's result. It also
does error handling in the way one would expect: just use try/except
around the yield.

Be *very careful* when using this functionality, as every time you do
"yield" you are doing a context switch. Random other code will have run
for an indeterminate amount of time, possibly changing attributes and
state on objects the generator has references to.


More information about the Twisted-Python mailing list