Giter VIP home page Giter VIP logo

aiopubsub's Introduction

aiopubsub - asyncio publish-subscribe within a process

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').

aiopubsub's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.