[Twisted-Python] Question about Using Deferreds in ServerProtocol request Handler

Jean-Paul Calderone exarkun at divmod.com
Wed Feb 20 09:18:28 MST 2008


On Wed, 20 Feb 2008 07:51:41 -0800 (PST), Andrew Francis <andrewfr_ice at yahoo.com> wrote:
>Hi Folks:
>
>Much of what I do with Stackless involves making calls
>from server requests that in turn, make Twisted calls
>that return a deferred.
>
> [snip]
>
>I have been able to do the same thing with an
>inlineCallback
>
>class MyRequestHandler(http.Request):
>
>    @defer.inlineCallbacks
>    def process(self):
>        try:
>            result = yield client.getPage("http://www.google.com")
>        except Exception, err:
>            log.err(err, "process getPage call failed")
>        else:
>            self.setHeader('Content-Type', 'text/html')
>            self.write(result)
>            self.finish()
>
>(although this works, the technique is not general for
>all protocols)
>
>However, how is this done with just deferreds and
>callbacks without Stackless or inlineCallbacks?

inlineCallbacks just allows a syntactic transformation.  The above
definition of process is identical to this one:

    def process(self):
        def callback(result):
            self.setHeader('Content-Type', 'text/html')
            self.write(result)
            self.finish()
        def errback(err):
            err.trap(Exception)
            log.err(err, "process getPage call failed")
        try:
            d = client.getPage("http://www.google.com")
        except Exception:
            d = failure.Failure()
        d.addCallbacks(callback, errback)

Exactly the same thing happens in either case.  The difference is
that inlineCallbacks lets you implement the callback as the section
of a generator function after a yield statement and the errback as
the suite of an except statement enclosing a yield expression in the
generator function.  The transformation is almost (or maybe actually)
a mechanical one which just changes the structure of the code, not the
behavior it implements.  inlineCallbacks is still based on Deferreds,
they're just obscured from view slightly.

Jean-Paul




More information about the Twisted-Python mailing list