Reputation: 715
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
Upvotes: 4
Views: 199
Reputation: 31890
The hypothetical connectToRemoteServer
API is going to hold a reference to d
internally, usually with a reference from the global reactor
to an object which will fire d
(by calling d.callback
) when the operation represented by d
is complete.
So the reference goes from the stack, to the stack frame for reactor.run
(because the reactor is running), to d
.
Upvotes: 2