[Twisted-Python] Gc of deferreds

glyph at divmod.com glyph at divmod.com
Tue Jul 8 16:39:10 EDT 2008

On 08:11 pm, benjamin.rutt at gs.com wrote:
>In the following program, what prevents the deferred "d" from being
>gc'd?  (nothing saves the return of foo() in any variable).  Do I need
>to keep a reference to it around to ensure the callbacks will run?

The reactor has a reference to a process transport.

The process transport has a reference to its process protocol, 
specifically a twisted.internet.utils._BackRelay.

The process protocol has a reference to the Deferred.

When the reactor learns that there is some information available for the 
process transport, it relays it to the transport which relays it to the 
protocol which may deliver it to the Deferred.

When the event fires which causes the Deferred to be called back, some 
of these references are broken, and it can generally be GC'd (unless 
there are more references or more callbacks).
>I am just questioning the need to keep deferreds in memory somewhere, 
>they haven't fired their callbacks yet.  Thanks.

I don't understand this question.  If the Deferred isn't in memory, then 
the callback will never be called.  All Deferreds which have interesting 
effects hang around for a while until those callbacks are called.  Are 
you suggesting that the Deferred get written out to disk or something, 
so it can be removed from memory?  There are a whole lot of reasons why 
*that* doesn't make any sense :).

More information about the Twisted-Python mailing list