Skip to content Skip to sidebar Skip to footer

How To Assign A Returned Value From The Defer Method In Python/twisted

I have a class, which is annotated as @defer.inlineCallbacks (I want to return the machine list from this) @defer.inlineCallbacks def getMachines(self): serverip = 'xx

Solution 1:

inlineCallbacks is just an alternate API for working with Deferred.

You've mostly successfully used inlineCallbacks to avoid having to write callback functions. You forgot to use returnValue though. Replace:

yield self.machineList

with

defer.returnValue(self.machineList)

This does not fix the problem you're asking about, though. inlineCallbacks gives you a different API inside the function it decorates - but not outside. As you've noticed, if you call a function decorated with it, you get a Deferred.

Add a callback (and an errback, eventually) to the Deferred:

returned = LdapClass().getMachines()
def report_result(result):
    print "The result was", result
returned.addCallback(report_result)
returned.addErrback(log.err)

Or use inlineCallbacks some more:

@inlineCallbacks
def foo():
    try:
        returned = yield LdapClass().getMachines()
        print "The result was", returned
    except:
        log.err()

Post a Comment for "How To Assign A Returned Value From The Defer Method In Python/twisted"