2
This example demonstrates how to use the arg1 messaging protocol, in the
3
original pubsub version 1.
5
There are several listeners created for three topics: topic1, topic2, and
6
"everything". A couple of functions are created that send messages.
8
The main constraints are:
10
- only the second argument in the sendMessage() can carry message
11
data; extra named (i.e. optional) parameters are possible
12
but will never receive data via pubsub. To send more data,
13
you would have to package it into a tuple or a class instance
14
and pass it as second argument.
15
- all topics along a topic branch get the same data; this doesn't
16
make much sense in general since typically only subtopics need extra
17
data, the parent topics wouldn't know what to do with the extra
19
- listeners must be callable with one argument, no more and
20
no less. The argument name can be anything.
22
:copyright: Copyright 2006-2009 by Oliver Schoenborn, all rights reserved.
23
:license: BSD, see LICENSE.txt for details.
27
from pubsub import setupv1
28
from pubsub import Publisher as pub
30
# ------------ create some listeners --------------
32
def listener1(msg, extra=None):
33
print 'Function listener1 received', msg.data
36
def onTopic1(self, msg, extra=None):
37
print 'Method Listener.onTopic1 received', `msg.data`
38
def onTopic2(self, msg):
39
print 'Method Listener.onTopic2 received', `msg.data`
40
def __call__(self, msg):
41
print 'Listener() received', `msg.data`
43
# ------------ register listeners ------------------
45
pub.subscribe(listener1, 'topic1')
47
listener2 = Listener()
48
pub.subscribe(listener2) # ALL topics!
49
pub.subscribe(listener2.onTopic1, 'topic1')
50
pub.subscribe(listener2.onTopic2, 'topic2')
52
# ------------ create a couple of senders --------------
55
print '--- SENDING topic1 message ---'
56
pub.sendMessage('topic1', 'message1')
57
print '---- SENT topic1 message ----'
60
print '--- SENDING topic2 message ---'
61
pub.sendMessage('topic2', 123)
62
print '---- SENT topic2 message ----'
64
# --------- define the main part of application --------
67
'''Loop until we get a quit message or user breaks.'''
73
if __name__ == '__main__':
b'\\ No newline at end of file'