Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

Simple pubsub pattern for asyncio applications

Project description

Simple pubsub pattern for asyncio applications.

aiopubsub is only tested with Python 3.6. There are no plans to support older versions.

import asyncio
import aiopubsub
import logwood

async def main():
        logwood.basic_config()
        hub = aiopubsub.Hub()
        publisher = aiopubsub.Publisher(hub, prefix = aiopubsub.Key('a'))
        subscriber = aiopubsub.Subscriber(hub, 'subscriber_id')

        sub_key = aiopubsub.Key('a', 'b', '*')
        subscriber.subscribe(sub_key)

        pub_key = aiopubsub.Key('b', 'c')
        publisher.publish(pub_key, 'Hello subscriber')
        await asyncio.sleep(0.001) # Let the callback fire.
        # "('a', 'b', 'c') Hello subscriber" will be printed.

        key, message = await subscriber.consume()
        assert key == aiopubsub.Key('a', 'b', 'c')
        assert message == 'Hello subscriber'

        subscriber.remove_all_listeners()


asyncio.get_event_loop().run_until_complete(main())

or, instead of directly subscribing to the key, we can create a listener that will call a synchronous callback when a new message arrives.

def print_message(key, message):
        print(key, message)

subscriber.add_sync_listener(sub_key, print_message)

Or, if we have a coroutine callback we can create an asynchronous listener:

async def print_message(key, message):
        await asyncio.sleep(1)
        print(key, message)

subscriber.add_async_listener(sub_key, print_message)

Aiopubsub will use logwood if it is installed, otherwise it will default to the standard logging module. Note that logwood is required to run tests.

Architecture

Hub accepts messages from Publishers and routes them to Subscribers. Each message is routed by its Key - an iterable of strings forming a hierarchic namespace. Subscribers may subscribe to wildcard keys, where any part of the key may be replaced replaced with a * (star).

addedSubscriber and removedSubscriber messages

When a new subscriber is added the Hub sends this message

{
        "key": ("key", "of", "added", "subscriber"),
        "currentSubscriberCount": 2
}

under the key ('Hub', 'addedSubscriber', 'key', 'of', 'added', 'subscriber') (the part after addedSubscriber is made of the subscribed key). Note the currentSubscriberCount field indicating how many subscribers are currently subscribed.

When a subscriber is removed a message in the same format is sent, but under the key ('Hub', 'removedSubscriber', 'key', 'of', 'added', 'subscriber').

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
aiopubsub-2.1.5.tar.gz (7.8 kB) Copy SHA256 hash SHA256 Source None May 7, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page