There are two setup modules for pubsub; only one can be imported in an application.
Setup pubsub for the kwargs message protocol. In a default installation this is the default protocol so this module is only needed if setupkargs utility functions are used, or in a custom installation where kwargs is not the default messaging protocol (such as in some versions of wxPython).
This module must be imported before the first from pubsub import pub statement in the application. Once :mod:pub has been imported, the messaging protocol cannot be changed (i.e., importing it after the first from pubsub import pub statement has undefined behavior).
The kwargs protocol is defined as follows:
Example:
from pubsub import pub
def listener1(msg):
assert msg == 'hi'
def listener(r1, o1=None):
assert r1 == 123
assert o1 is None
pub.subscribe(listener1, 'someTopic_1')
pub.subscribe(listener2, 'someTopic_2')
pub.sendMessage('someTopic_1', msg='hi')
pub.sendMessage('someTopic_2', r1=123) # ok: o1 optional
Utility function to assist migrating an application from using the arg1 messaging protocol to using the kwargs protocol. Call this after having run and debugged your application with setuparg1.enforceArgName(commonName). See the migration docs for more detais.
Setup pubsub for the arg1 message protocol. In a default pubsub installation the default protocol is kargs.
This module must be imported before the first from pubsub import pub statement in the application. Once :mod:pub has been imported, the messaging protocol must not be changed (i.e., importing it after the first from pubsub import pub statement has undefined behavior).
from .. import setuparg1
from .. import pub
The arg1 protocol is identical to the legacy messaging protocol from first version of pubsub (when it was still part of wxPython) and is deprecated. This module is therefore deprecated.
The arg1 protocol is defined as follows:
An important consequence of the freedom of this protocol is that the same listener can subscribe to any topic; it is the listener’s responsibility to determine what data is packaged in message.data and use properly.
Example:
from pubsub import setuparg1
from pubsub import pub
def listener1(msg):
assert msg.data == 'hi'
def listener2(msg):
assert msg.data['r1'] == 123
pub.subscribe(listener1, 'someTopic')
pub.subscribe(listener2, 'someTopic') # same topic ok despite differing message data
pub.sendMessage('someTopic', 'hi')
data = {'r1':123, ...} # replace ... with whatever you want
pub.sendMessage('someTopic', data)
This will configure pubsub to require that all listeners use the same argument name (commonName) as first parameter. This is a ueful first step in migrating an application that has been using arg1 protocol to the more powerful kwargs protocol.