Bubus Event Bus

1.5.6 · active · verified Fri Apr 10

Bubus is an advanced, Pydantic-powered asynchronous event bus library for Python. It facilitates decoupled communication between components by allowing them to publish and subscribe to events. It's designed for modern Python applications, emphasizing type safety, performance, and robust error handling. The current version is 1.5.6, and it receives regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an `EventBus`, define a `BaseEvent` with Pydantic, subscribe an asynchronous handler to it, and publish an event. It then shows how to wait for all handlers to complete and gracefully stop the bus.

import asyncio
from bubus import EventBus, BaseEvent

class MyEvent(BaseEvent):
    message: str

async def my_handler(event: MyEvent) -> None:
    print(f"Received event: {event.message}")
    await asyncio.sleep(0.01) # Simulate async work

async def main():
    bus = EventBus()
    bus.subscribe(MyEvent, my_handler)

    print("Publishing event...")
    await bus.publish(MyEvent(message="Hello from Bubus!"))

    # Wait for all published events to be handled
    await bus.wait_for_all_handlers()
    await bus.stop() # Gracefully shut down the event bus
    print("Bus stopped.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →