Skip to main content

Type-safe async event bus for Python

Project description

busify

Type-safe async event bus for Python.

Installation

pip install busify

Quick Start

import asyncio
from dataclasses import dataclass
from busify import EventBus, BaseEvent

@dataclass(kw_only=True, frozen=True)
class UserCreatedEvent(BaseEvent[None]):
    user_id: str
    email: str

bus = EventBus()

async def send_welcome_email(event: UserCreatedEvent):
    print(f"Sending welcome email to {event.email}")

bus.subscribe(UserCreatedEvent, send_welcome_email)

async def main():
    event = UserCreatedEvent(user_id="123", email="user@example.com")
    await bus.dispatch(event)

asyncio.run(main())

Core Concepts

Events

Events are immutable dataclasses that can optionally carry results:

@dataclass(frozen=True)
class ScreenshotResult:
    data: bytes

@dataclass(kw_only=True, frozen=True)
class CaptureScreenshotEvent(BaseEvent[ScreenshotResult]):
    quality: int = 90

Handlers

Handlers are async functions that process events:

async def capture_handler(event: CaptureScreenshotEvent):
    data = await take_screenshot(quality=event.quality)
    event.set_result(ScreenshotResult(data=data))

bus.subscribe(CaptureScreenshotEvent, capture_handler)

Dispatching

Dispatch events and retrieve results:

event = CaptureScreenshotEvent(quality=95)
await bus.dispatch(event)
result = event.get_result()

API Reference

EventBus

  • subscribe(event_type, handler) - Register handler for event
  • unsubscribe(event_type, handler) - Remove handler
  • unsubscribe_all(event_type=None) - Clear handlers
  • dispatch(event) - Run all handlers for event
  • wait_for_event(event_type, timeout=None, predicate=None) - Wait for event
event = await bus.wait_for_event(
    UserCreatedEvent,
    timeout=5.0,
    predicate=lambda e: e.email.endswith("@example.com")
)

BaseEvent

  • id - Event identifier
  • timestamp - Creation time
  • is_completed - Finished?
  • has_error - Failed?
  • set_result(value) - Store result
  • get_result() - Retrieve result
  • set_exception(exc) - Mark as failed

Error Handling

Failed handlers don't crash the dispatch:

async def failing_handler(event: UserCreatedEvent):
    raise ValueError("Something went wrong")

async def working_handler(event: UserCreatedEvent):
    print("This still runs")

bus.subscribe(UserCreatedEvent, failing_handler)
bus.subscribe(UserCreatedEvent, working_handler)

event = UserCreatedEvent(user_id="123", email="user@example.com")
await bus.dispatch(event)

if event.has_error:
    event.get_result(raise_if_exception=True)  # Raises the exception

Requirements

Python 3.12+

License

MIT

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

busify-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

busify-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file busify-0.1.0.tar.gz.

File metadata

  • Download URL: busify-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for busify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ec968b22d01f7d99c5ebce10a8619b5f54af85ad863e324eebb76417704f4b2
MD5 2931544de896d213e04da461a1342ca7
BLAKE2b-256 2a4331c221d27cc97fc4c56c3912d909b95f78757a3f9399327f019590fc5926

See more details on using hashes here.

File details

Details for the file busify-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: busify-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for busify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6ffb4c96c42c9a648410ce7f170bffd89d2a713132b62ee5755a10aa496555b
MD5 6a4d7ced34642bf15042864951d42ef1
BLAKE2b-256 b495e303f1e5d23dc7e3057142f987594fd986c7287a4a98a5b927e1422541cb

See more details on using hashes here.

Supported by

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