bitween.components.pubsub package

Submodules

bitween.components.pubsub.autosubscriber module

Democlass for autosubscribing

needed for testing

class bitween.components.pubsub.autosubscriber.AutoSub[source]

Bases: bitween.components.pubsub.pubsub.Subscriber

Democlass for autosubscribing

needed for testing

on_some_topic(some_string, some_int=1)[source]
process_messages()[source]

process messages

since this class autosubsribes topics, topic “some_topic” will resolve to “on_some_topic()”

Returns:

bitween.components.pubsub.pubsub module

class bitween.components.pubsub.pubsub.Subscriber(name='', autosubscribe=False)[source]
get_message(timeout=0.1)[source]

get topic, arguments and names arguments

Parameters:timeout
Returns:topic, args, kwargs
Return type:str, list, dict
has_messages()[source]
Returns:True if Queue not Empty, otherwise False
publish(topic, *args, **kwargs)[source]

just for nicer logging output. calls the regular publish via this method

Parameters:
  • topic
  • args
  • kwargs
Returns:

subscribe(topic)[source]

manually subscribe a topic

Parameters:topic
Returns:
bitween.components.pubsub.pubsub.publish(topic, *args, **kwargs)[source]

publish arguments to a topic.

Parameters:
  • topic (str) – topic to publish the message to
  • args – unnamed arguments
  • kwargs – named arguments
Returns:

False if no Subscribers, else True

Module contents

PubSub for interprocess communication

holds topics to subscribe and methods to publish to those topics.

usage

basic:

# create the subscriber object ans subscribe to topic 'myTopic'

s = Subscriber()
s.subscribe('myTopic')

def loop():
    while True:
        if s.has_messages():
            (topic, args, kwargs) = s.get()
            print('%s, %s, %s' % topic, args, kwargs)

# (maybe from another thread and object) post to the topic:

publish('myTopic', 'somestring', val=123)

by subclassing:

class MySub(Subscriber):
    def __init__(self):
        super().__init__()
        self.subscribe('myTopic')
        self.loop()

    def loop():
        while True:
            if self.has_messages():
                (topic, args, kwargs) = self.get()
                print('%s, %s, %s' % topic, args, kwargs)