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 ?
Your call to
self.Connect()
inDoSomething()
is supposed to return a not-yet-fired Deferred; that's how inlineCallbacks works. The code at the beginning ofConnect()
should have been called, but once it hit the firstyield
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 thec
Deferred fires, and you don't want to makeDoSomething()
into another inlineCallbacks method, then just use the Deferred in the normal way:addCallback()
and friends. Example:For more information on how Twisted Deferreds work, see http://twistedmatrix.com/documents/current/core/howto/defer.html . Hope this helps.