PyDispatcher question

Jorgen Bodde jorgen.maillist at gmail.com
Thu Apr 5 14:35:11 EDT 2007


Hi Mike,

Thanks for the explanation! The Python shell really threw me off here.
I thought it would work exactly the same as in the interpreter itself.
:-)

Even assigning None to the object exibits the same behaviour as "del"
does. COOL!

This is what I did sofar:

from pydispatch import dispatcher

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

a = X()
dispatcher.send(1)

a = None

dispatcher.send(1)

print '----------------------'

b = X()
c = X()
dispatcher.send(1)

With as output;

C:\Documents and Settings\Jorg\Desktop>python New1.py
Hello world
----------------------
Hello world
Hello world

Nice! Thanks a lot for the info!

- Jorgen


On 4/5/07, Mike C. Fletcher <mcfletch at vrplumber.com> wrote:
> 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
> #gc.collect()
>
> 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.
>
> HTH,
> Mike
>
> --
> ________________________________________________
>   Mike C. Fletcher
>   Designer, VR Plumber, Coder
>   http://www.vrplumber.com
>   http://blog.vrplumber.com
>
>



More information about the Python-list mailing list