- This method may be
- called multiple times for one listener, registering it with
- many topics. It can also be invoked many times for a
- particular topic, each time with a different listener.
- See the class doc for requirements on listener and topic.
-
- :note: Calling
- this method for the same listener, with two topics in the same
- branch of the topic hierarchy, will cause the listener to be
- notified twice when a message for the deepest topic is sent. E.g.
- subscribe(listener, 't1') and then subscribe(listener, ('t1','t2'))
- means that when calling sendMessage('t1'), listener gets one message,
- but when calling sendMessage(('t1','t2')), listener gets message
- twice. This effect could be eliminated but it would not be safe to
- do so: how do we know what topic to give the listener? Answer appears
- trivial at first but is far from obvious. It is best to rely on the
- user to be careful about who registers for what topics.
+ This method may be called multiple times for one listener,
+ registering it with many topics. It can also be invoked many
+ times for a particular topic, each time with a different
+ listener. See the class doc for requirements on listener and
+ topic.
+
+ :note: The listener is held by Publisher() only by *weak*
+ reference. This means you must ensure you have at
+ least one strong reference to listener, otherwise it
+ will be DOA ("dead on arrival"). This is particularly
+ easy to forget when wrapping a listener method in a
+ proxy object (e.g. to bind some of its parameters),
+ e.g.::
+
+ class Foo:
+ def listener(self, event): pass
+ class Wrapper:
+ def __init__(self, fun): self.fun = fun
+ def __call__(self, *args): self.fun(*args)
+ foo = Foo()
+ Publisher().subscribe( Wrapper(foo.listener) ) # whoops: DOA!
+ wrapper = Wrapper(foo.listener)
+ Publisher().subscribe(wrapper) # good!
+
+ :note: Calling this method for the same listener, with two
+ topics in the same branch of the topic hierarchy, will
+ cause the listener to be notified twice when a message
+ for the deepest topic is sent. E.g.
+ subscribe(listener, 't1') and then subscribe(listener,
+ ('t1','t2')) means that when calling sendMessage('t1'),
+ listener gets one message, but when calling
+ sendMessage(('t1','t2')), listener gets message twice.
+