Python PubSub Website

Site Contents

Pubsub’s Pub Module

An application that wishes to make use of pubsub’s main functionality should import pubsub’s pub module:

from pubsub import pub

The pub module supports:

  • publishing and receiving messages of a given topic
  • tracking pubsub activity in an application
  • dealing with “badly behaved” listeners (ie that leak exceptions)
  • specifying or just documenting the topic tree of an application


the signature of functions of pub module, as seen in this manual, typically have self as first function parameter. Omit this parameter in your calls, as it is provided for you by Python (so it will not be described in the text). For instance for pub.subscribe the signature is subscribe(self, listener, topicName), but it must be called as though it were subscribe(listener, topicName).

Publishing Messages

“Publishing” and “Sending a message to the rest of your application” are the same thing in this documentation.

To publish a message, it is sufficient to use the pub.sendMessage() function:

sendMessage(topicName, **kwargs)

Send a message.

  • topicName – name of message topic (dotted or tuple format)
  • kwargs – message data (must satisfy the topic’s MDS)

For example, to send a message of topic ‘topicA’ with two data items, arg1 and arg2:

pub.sendMessage('topicA', arg1=123, arg2='abc')

For the task of publishing messages, here are some important concepts:

  • Topics are akin to classes in OOP, or to channels in radio broadcasting; similar to classes, they can be organized into a hierarchy, known as the “topic tree” of the application.
  • Topic names are strings or tuples of strings.
  • MDS: Each topic defines what message data is allowed or required for messages of that topic. This is the Message Data Specification, or MDS. Subtopics inherit the MDS of their parent topic.

More details can be found on the following pages:

Receiving Messages

In general, listener-related concepts are explained in the section: