Python PubSub Website

Site Contents

Legacy (v1) API

This section describes Version 1 of the pubsub API (versions are discussed in API Versions section). Note that this API is deprecated!! Only use it if you have no choice. See Upgrade from v1 for how to upgrade your application to use the new API.

The pubsub package must be configured for version 1 API before any other pubsub module is imported from it, by using the pubsub.setupv1 module from the top-level script of your application:

from pubsub import setupv1

This can then be followed by

from pubsub import Publisher

in the same module. Other modules in your application do not need to import setupv1.


Provides “version 1” of pubsub’s publish-subscribe API.

Sending a message is as simple as calling a function with a topic name and (optionally) some data that will be sent to the listeners.

Any callable object can be a message listener, if it can be called with one parameter. Example use:

from pubsub import Publisher
def aCallable(msg):
    print 'data received',
Publisher.subscribe(aCallable, 'some_topic')
Publisher.sendMessage('some_topic', data=123)
// should see 'data received 123'

Note: in this documentation, pubsub version 1 is assumed to be part of wxPython’s wx.lib package. There isn’t a reason for using version 1 other than for legacy wxPython applications. Even then, consider upgrading your wxPython application to the latest API, as explained on the pubsub website.

The three important concepts for pubsub are:

  • listener: a function, bound method or callable object that can be called with one parameter (not counting ‘self’ in the case of methods). For instance, these listeners are ok:

    class Foo:
        def __call__(self, a, b=1): pass # can be called with only one arg
        def meth(self,  a):         pass # takes only one arg
        def meth2(self, a=2, b=''): pass # can be called with one arg
    def func(a, b=''): pass
    Foo foo
    import pubsub as Publisher
    Publisher.subscribe(foo)           # functor
    Publisher.subscribe(foo.meth)      # bound method
    Publisher.subscribe(foo.meth2)     # bound method
    Publisher.subscribe(func)          # function

    The three types of callables all have signature that allows a call with only one argument. When called, the argument will be a reference to a pub.Message object, containing both the data and topic name of the message. In every example above, the ‘’ will contain the message data, ‘a.topic’ will contain the topic name.

  • topic: a single word, a tuple of words, or a string containing a set of words separated by dots, for example: ‘’. A tuple or a dotted notation string denotes a hierarchy of topics from most general to least. For example, a listener of this topic:


    would receive messages for these topics:

    '               # because same
    ''   # because more specific

    but not these:

    'sports'      # because more general
    'news'        # because different topic
  • message: this is an instance of pub.Message, with self.topic referring to the topic name for which the message was sent, and referring to the data given to sendMessage().

Note that an important feature of pubsub is that as soon as a listener is no longer in use in your application (except for being subscribed to pubsub topics), pubsub will automatically unsubscribe it. See PublisherClass docs for more details.

Author:Oliver Schoenborn
Copyright:Copyright since 2006 by Oliver Schoenborn, all rights reserved.
License:BSD, see LICENSE.txt for details.

The Publisher


Singleton instance of pub.PublisherClass. Methods of this class are described next.

class PublisherClass(singletonKey)

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.


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')]

How many listeners have received a message since beginning of run


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

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


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

sendMessage(topic='', 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(listener, topic='')

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): = fun
    def __call__(self, *args):*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(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(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).


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


from pubsub import Publisher

def listenerAll(msg):
    print "listenerAll:      topic=", msg.topic
def listenerSubTopic(msg):
    print "listenerSubTopic: topic=", msg.topic

Publisher.subscribe(listenerAll, Publisher.ALL_TOPICS)
Publisher.sendMessage('some_topic.sub_topic', data=123)
# will print
# listenerAll:      topic=some_topic.sub_topic
# listenerSubTopic: topic=some_topic.sub_topic

Other objects in pub

It is unlikely that you will need the pub module directly. The Publisher described in the previous section is an instance of pub.PublisherClass. The pub module also contains:

class Message(topic, data)

A simple container object for the two components of a message: the topic and the user data. Each listener called by sendMessage(topic, data) gets an instance of Message. The given ‘data’ is accessed via, while the topic name is available in Message.topic:

def listener(msg):
    print "data is ",
    print "topic name is ", msg.topic
    print msg

The example shows how a message can be converted to string.


Function to call if, for whatever reason, you need to know explicitely what is the string to use to indicate ‘all topics’.


The string that refers to the root topic of the topic tree hierarchy. Listeners of this topic receive all messages. This data is also an attribute of Publisher.


from pubsub import pub