PyDispatcher question

Mike C. Fletcher mcfletch at
Thu Apr 5 20:08:47 CEST 2007

Jorgen Bodde wrote:
> Hi all,
> Hopefully someone can help me. I am fairly new to Python, and I am
> looking into PyDispatcher. I am familiar with the C++ sigslot variant,
> and I wonder how similar PyDispatches is. I run in to the following
> 'problem' (pseudo code, untested here)
Here's some real code...

from pydispatch import dispatcher
import gc

class X(object):
    def __init__( self ):
        dispatcher.connect( self.someSignal, signal=1 )
    def someSignal( self ):
        print 'hello world'

obj = X()
dispatcher.send( signal= 1 )

del obj

dispatcher.send( signal= 1 )

This will print out only one "hello world" on my Python 2.5 Gentoo 
machine (it should work the same on any recent Python).  Basically your 
python shell will tend to keep around an extra copy of the X instance 
until you get rid of it explicitly, and that's what keeps the object 
"live" and receiving signals if you try the code in the shell.  
PyDispatcher is designed so that, by default, when the object goes away 
the registration is removed.  It uses the weakref module to do this, 
rather than __del__ methods, to avoid garbage cycles, btw.


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list