Skip to main content

Internal application publisher/subscriber bus using asyncio queues.

Project description

Asyncio Signal Bus

Asyncio Signal Bus is a simple utility for sending publisher subscriber signals from one python function to another, thus allowing you to simplify asynchronous applications using event based architecture.

Installation

Install with pip.

pip install asyncio-signal-bus

Usage

Simply decorate your publishers and subscribers to pass any data type asynchronously whenever the publisher returns a value.

import asyncio

from asyncio_signal_bus import SignalBus
import json
from typing import Dict

BUS = SignalBus()

@BUS.publisher(topic_name="foo")
async def foo_publisher(arg: str):
    signal = {"message": arg}
    print(f"publishing {json.dumps(signal)}")
    await asyncio.sleep(1)
    return signal

@BUS.subscriber(topic_name="foo")
async def foo_subscriber_0(signal: Dict):
    print(f"foo subscriber 0 received {json.dumps(signal)}")

@BUS.subscriber(topic_name="foo")
async def foo_subscriber_1(signal: Dict):
    print(f"foo subscriber 1 received {json.dumps(signal)}")

In order to start the subscribers listening, you use the bus context. When the bus leaves context, all subscribers stop listening.

async def main():
    inputs = [f"message:{i}" for i in range(10)]
    async with BUS:
        await asyncio.gather(*[foo_publisher(x) for x in inputs])

That is it! You can string together publishers and subscribers in whatever way you want. You can also annotate functions and methods used for other purposes as publishers in order to facilitate post processing and reporting hooks without having to pass them to the function.

Dependency Injection

Subscribers often need additional data beyond the signal, such as URL's, secrets, usernames and connection pools. The inject decorator can be used to inject additional data to other arguments as kwargs defaults. Injection only occors when the bus or it's injector is within context.

import asyncio

from asyncio_signal_bus import SignalBus
BUS = SignalBus()

@BUS.publisher(topic_name="greeting")
async def generate_greeting(arg: str):
    return arg

async def name_factory():
     return "Frank"

@BUS.subscriber(topic_name="greeting")
@BUS.inject("name", name_factory)
async def print_greeting(greeting: str, name: str):
    print(f"{greeting} from {name}")
async def main():
    async with BUS:
        await generate_greeting("hello")
asyncio.run(main())

Periodic Tasks

Sometimes we need to run tasks periodically such as polling an external database or an internal queue. We also want to make sure that when our application shuts down, it gracefully tells the periodic tasks to stop starting new tasks but it waits for the existing task to complete.

import asyncio
from asyncio_signal_bus import SignalBus

BUS = SignalBus()
@BUS.periodic_task(period_seconds=0.5)
async def print_foos():
     print("foo")
     
async def main():
     async with BUS:
         await asyncio.sleep(2)
asyncio.run(main())

Period Tasks As Publishers

Given that the periodic task is the furthest upstream in a process, we may use it to drive downstream subscribers. We can combine the periodic task with the injectors and the publisher to periodically fetch data using configuration information and send it to subscribers for processing.

import asyncio
from typing import List, Dict
from asyncio_signal_bus import SignalBus
import os

BUS = SignalBus()

async def get_url():
    return os.environ.get("URL")

async def get_secret():
    return os.environ.get("SECRET")

@BUS.periodic_task(period_seconds=1)
@BUS.publisher(topic_name="new-data")
@BUS.inject("url", get_url)
@BUS.inject("secret", get_secret)
async def get_data(url: str, secret: str):
    # Perform some sort of IO here to get your data.
    return [
        {"id": 0, "value": "cats"},
        {"id": 1, "value": "dogs"}
    ]

@BUS.subscriber(topic_name="new-data")
async def process_values(data: List[Dict]):
    for row in data:
        print(row["value"].upper())

@BUS.subscriber(topic_name="new-data")
async def process_ids(data: List[Dict]):
    for row in data:
        print(row["id"] + 1)

async def main():
    async with BUS:
        await asyncio.sleep(5)
asyncio.run(main())

Project details


Download files

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

Source Distribution

asyncio_signal_bus-1.5.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

asyncio_signal_bus-1.5.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_signal_bus-1.5.2.tar.gz.

File metadata

  • Download URL: asyncio_signal_bus-1.5.2.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for asyncio_signal_bus-1.5.2.tar.gz
Algorithm Hash digest
SHA256 8c50ed6bfced48c4aa77869d4f0bcbc7ffebcbdad38378d6dc2b7d51a7104252
MD5 d81760e6329aa53513cb28c50b643c36
BLAKE2b-256 9c33eb47ffffa00593acbd5aae5418696e9c643c54322126ce677424c07466ec

See more details on using hashes here.

File details

Details for the file asyncio_signal_bus-1.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_signal_bus-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 255fe964ffbef3eb8d6129d00b7384de67ac8396ce270bf42ec0768217b3d410
MD5 8490cc441ec202ab0ac07858b16fa2b3
BLAKE2b-256 a7c78f8dc72c56f2044af541f8d3a432f43c256f5772c5077b3931f84c7196a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page