If I create a Deferred and add some callbacks to it while in the reactor event loop, will it ever be called if I let the local reference go out of scope? For example, if I have a protocol with a connectionMade() like so:
def connectionMade(self):
# This made up function returns a deferred that will connect to
# some remote server using some made up protocol and return some data.
d = connectToRemoteServer(reactor, url)
d.addCallback(self._handleRemoteConnection)
# At this point, d is going to go out of scope, so will the deferred
# it points to ever get fired?
def _handleRemoteConnection(self, data):
# Do something with the data
I have seen this pattern in different code that uses Twisted, and I am having trouble understanding why the deferred that is returned from connectToRemoteServer() is not
garbage collected when d goes out of scope. I would think it would never fire, or maybe fail randomly due to a race condition. Can anyone explain to me why this works? I have read http://twistedmatrix.com/documents/current/core/howto/defer.html a couple times, but I still am not sure why this would work?
Thanks,
Carl
The hypothetical
connectToRemoteServerAPI is going to hold a reference todinternally, usually with a reference from the globalreactorto an object which will fired(by callingd.callback) when the operation represented bydis complete.So the reference goes from the stack, to the stack frame for
reactor.run(because the reactor is running), tod.