API Reference for Enthought Tool Suite 3.2.0
Implements a simple, robust, safe, Messenger class that allows one to register callbacks for a signal/slot (or event/handler) kind of messaging system. One can basically register a callback function/method to be called when an object sends a particular event. The Messenger class is Borg. So it is easy to instantiate and use. This module is also reload-safe, so if the module is reloaded the callback information is not lost. Method callbacks do not have a reference counting problem since weak references are used.
The main functionality of this module is provided by three functions, connect, disconnect and send.
Here is example usage with VTK:
>>> import messenger, vtk
>>> def cb(obj, evt):
... print obj.__class__.__name__, evt
...
>>> o = vtk.vtkProperty()
>>> o.AddObserver('ModifiedEvent', messenger.send)
1
>>> messenger.connect(o, 'ModifiedEvent', cb)
>>>
>>> o.SetRepresentation(1)
vtkOpenGLProperty ModifiedEvent
>>> messenger.connect(o, 'AnyEvent', cb)
>>> o.SetRepresentation(2)
vtkOpenGLProperty ModifiedEvent
vtkOpenGLProperty ModifiedEvent
>>>
>>> messenger.send(o, 'foo')
vtkOpenGLProperty foo
>>> messenger.disconnect(o, 'AnyEvent')
>>> messenger.send(o, 'foo')
>>>
This approach is necessary if you don't want to be bitten by reference cycles. If you have a Python object holding a reference to a VTK object and pass a method of the object to the AddObserver call, you will get a reference cycle that cannot be collected by the garbage collector. Using this messenger module gets around the problem.
Also note that adding a connection for 'AnyEvent' will trigger a callback no matter what event was generated. The code above also shows how disconnection works.
| Local name | Refers to |
|---|---|
| sys | sys |
| types | types |
| weakref | weakref |
© Copyright 2002-2009 Enthought, Inc.