[Twisted-Python] serializing inline callbacks

Dustin J. Mitchell dustin at v.igoro.us
Tue Feb 18 12:03:27 MST 2014


I'm suggesting that there are lots more problems with your code than
the one you have identified.  Any method which is decorated with
@defer.inlineCallbacks will return a Deferred, and you need to handle
every Deferred somehow.  Generally (although not always, as lvh has
said) you do that by yielding it.

Your code should be

----
    def render_GET(self, request):
        ...
        d = self.A()
        d.addErrback(request.processingFailed)
        return NOT_DONE_YET

    @inlineCallbacks
    def A(self):
        ...
        tmp = yield subprocess(args)
        ...
        yield self.B()

    @inlineCallbacks
    def B(self):
        ...
        tmp = yield subprocess(args)
        ...
        yield self.C()

    def C(self):
        ...
        x = yield self.D(...)
        y = yield self.D(...)
        z = yield self.D(...)
        ...
        yield self.E()

    @inlineCallbacks
    def D(self):
        ...
        tmp = yield subprocess(args)
        ...
        returnValue(tmp)


    @inlineCallbacks
    def E(self):
        ...
        tmp = yield subprocess(args)
        ...
        request.write(...)
        request.finished()

----

Here you see that all of the Deferreds are handled, either by yielding
them or, in the case of the Deferred from A, by adding an errback that
will conveniently format an HTML error response.

Dustin




More information about the Twisted-Python mailing list