Question

When i call DoSomething() method from my test method, i expect it to block on the yield in Connect() but it doesnt, it returns a not called yet deferred.

Class Foo:
    @defer.inlineCallbacks
    def Connect(self):
        amqpConfig = AmqpConfig(self.config.getConfigFile())
        self.amqp = AmqpFactory(amqpConfig)

        try:
            # First Deferred
            d = self.amqp.connect()
            # Second Deferred
            d.addCallback(lambda ign: self.amqp.getChannelReadyDeferred()) 

            # Block until connecting and getting an AMQP channel
            yield d
        except Exception, e:
            self.log.error('Cannot connect to AMQP broker: %s', e)

    def DoSomething(self):
        c = self.Connect()
        # None of the deferreds were fired, c.called is False

How can i let it block till the second (and last) deferred is called ?

Was it helpful?

Solution

Your call to self.Connect() in DoSomething() is supposed to return a not-yet-fired Deferred; that's how inlineCallbacks works. The code at the beginning of Connect() should have been called, but once it hit the first yield it just returned its Deferred to your caller. Later on, when the AMQP channel is acquired, that Deferred will fire.

If you want the DoSomething() call to... do something :) after the c Deferred fires, and you don't want to make DoSomething() into another inlineCallbacks method, then just use the Deferred in the normal way: addCallback() and friends. Example:

def DoSomething(self):
    c = self.Connect()
    c.addCallback(self.handle_getting_amqp_channel)
    c.addErrback(self.this_gets_called_if_something_went_wrong)
    return c

For more information on how Twisted Deferreds work, see http://twistedmatrix.com/documents/current/core/howto/defer.html . Hope this helps.

OTHER TIPS

Code seemes little unclear (especially 4th line indenting). But, if I get it right self.Connect() call returns a deferred itself, so You'll have to use:

c = yield self.Connect()

in order to prevent it from running in background and do return deferredResult in to c variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top