Table Of Contents

Previous topic

_TopicTreeRoot

Next topic

pubsubconf

This Page

phoenix_title PublisherClass

Class to manage publishing, subscribing and message sending.

Note: User code should not instantiate this class directly. Instead, a module attribute named ‘Publisher’ provides a unique instance of this class, ensuring that all modules use the same publisher.

The most important methods are sendMessage() and subscribe(). The remaining ones are likely helpful only for debugging an application’s message sending/receiving.


class_hierarchy Inheritance Diagram

Inheritance diagram for class PublisherClass

Inheritance diagram of PublisherClass


method_summary Methods Summary

__init__ Construct a Publisher. This can only be done by the pubsub
getAssociatedTopics Return a list of topics the given listener is registered with.
getDeliveryCount How many listeners have received a message since beginning of run
getMessageCount How many times sendMessage() was called since beginning of run
isSubscribed Return true if listener has subscribed to topic specified.
isValid Return true only if listener will be able to subscribe to
sendMessage Send a message for given topic, with optional data, to
subscribe Subscribe listener for given topic. If topic is not specified,
unsubAll Unsubscribe all listeners subscribed for topics. Topics can
unsubscribe Unsubscribe listener. If topics not specified, listener is
validate Similar to isValid(), but raises a TypeError exception if not valid

api Class API



class PublisherClass

Class to manage publishing, subscribing and message sending.

Note: User code should not instantiate this class directly. Instead, a module attribute named ‘Publisher’ provides a unique instance of this class, ensuring that all modules use the same publisher.

The most important methods are sendMessage() and subscribe(). The remaining ones are likely helpful only for debugging an application’s message sending/receiving.


Methods



__init__(self, singletonKey)

Construct a Publisher. This can only be done by the pubsub module. You just use pubsub.Publisher().



getAssociatedTopics(self, listener)

Return a list of topics the given listener is registered with. Returns [] if listener never subscribed.

Attention :

when using the return of this method to compare to expected list of topics, remember that topics that are not in the form of a tuple appear as a one-tuple in the return. E.g. if you have subscribed a listener to ‘topic1’ and (‘topic2’,’subtopic2’), this method returns:

associatedTopics = [('topic1',), ('topic2','subtopic2')]


getDeliveryCount(self)

How many listeners have received a message since beginning of run



getMessageCount(self)

How many times sendMessage() was called since beginning of run



isSubscribed(self, listener, topic=None)

Return true if listener has subscribed to topic specified. If no topic specified, return true if subscribed to something. Use topic=getStrAllTopics() to determine if a listener will receive messages for all topics.



isValid(self, listener)

Return true only if listener will be able to subscribe to Publisher.



sendMessage(self, topic=ALL_TOPICS, data=None, onTopicNeverCreated=None)

Send a message for given topic, with optional data, to subscribed listeners. If topic is not specified, only the listeners that are interested in all topics will receive message. The onTopicNeverCreated is an optional callback of your choice that will be called if the topic given was never created (i.e. it, or one of its subtopics, was never subscribed to by any listener). It will be called as onTopicNeverCreated(topic).



subscribe(self, listener, topic = ALL_TOPICS)

Subscribe listener for given topic. If topic is not specified, listener will be subscribed for all topics (that listener will receive a Message for any topic for which a message is generated).

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 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.



unsubAll(self, topics=None, onNoSuchTopic=None)

Unsubscribe all listeners subscribed for topics. Topics can be a single topic (string or tuple) or a list of topics (ie list containing strings and/or tuples). If topics is not specified, all listeners for all topics will be unsubscribed, ie. there will be no topics and no listeners left. If onNoSuchTopic is given, it will be called as onNoSuchTopic(topic) for each topic that is unknown.



unsubscribe(self, listener, topics=None)

Unsubscribe listener. If topics not specified, listener is completely unsubscribed. Otherwise, it is unsubscribed only for the topic (the usual tuple) or list of topics (ie a list of tuples) specified. Nothing happens if listener is not actually subscribed to any of the topics.

Note that if listener subscribed for two topics (a,b) and (a,c), then unsubscribing for topic (a) will do nothing. You must use getAssociatedTopics(listener) and give unsubscribe() the returned list (or a subset thereof).



validate(self, listener)

Similar to isValid(), but raises a TypeError exception if not valid