[Twisted-Python] Another inlinecallback question

Jean-Paul Calderone exarkun at divmod.com
Sat Jan 26 14:09:43 EST 2008


On Sat, 26 Jan 2008 10:02:41 -0800 (PST), Andrew Francis <andrewfr_ice at yahoo.com> wrote:
>Hello Matthias:
>
>>User defer.returnValue(result) instead of return.
>
>No I previously tried using defer.returnValue(), not
>expecting it to work, and it did not.
>
>The calling function (some method in PyAMF) is
>expecting a value from the return statement, a return
>that cannot be set with an argument.

Matthias's suggestion was the only correct one.  It seems you
want to use inlineCallbacks to do something which it cannot do.

Here's a tiny sample of something that's not possible:

    def asyncOp():
        result = yield getPage(...)
        yield returnValue(result)
    asyncOp = inlineCallbacks(asyncOp)

    def syncOp():
        result = asyncOp()
        print "The page is", result

However, this is possible:

    # define asyncOp as above

    def anotherAsyncOp():
        resultDeferred = asyncOp()
        def gotResult(result):
            print "The page is", result
        resultDeferred.addCallback(gotResult)
        return resultDeferred
        
In other words, inlineCallbacks lets you suspend execution, _inside_ the
function you decorate with it, until a result is available.  It only lets
you do this _inside_.  Outside, the decorated function simply appears to
return a Deferred.

Jean-Paul




More information about the Twisted-Python mailing list