[Twisted-Python] Gc of deferreds

Rutt, Benjamin Benjamin.Rutt at gs.com
Tue Jul 8 16:11:31 EDT 2008


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?



#!/usr/bin/env python
from twisted.internet import defer,reactor,utils

def foo():
    d = utils.getProcessOutput('sleep', args=['2'])
    def cb(data):
      print "cb fired"
      return data
    d.addCallback(cb)
    return d

foo()

reactor.callLater(5, reactor.stop)
reactor.run()



Yet when I run it, it seems everything works fine:

$ ./spawner.py 
cb fired
$ 

I am just questioning the need to keep deferreds in memory somewhere, if
they haven't fired their callbacks yet.  Thanks.




More information about the Twisted-Python mailing list