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 eventunsubscribe(event_type, handler)- Remove handlerunsubscribe_all(event_type=None)- Clear handlersdispatch(event)- Run all handlers for eventwait_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 identifiertimestamp- Creation timeis_completed- Finished?has_error- Failed?set_result(value)- Store resultget_result()- Retrieve resultset_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
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ec968b22d01f7d99c5ebce10a8619b5f54af85ad863e324eebb76417704f4b2
|
|
| MD5 |
2931544de896d213e04da461a1342ca7
|
|
| BLAKE2b-256 |
2a4331c221d27cc97fc4c56c3912d909b95f78757a3f9399327f019590fc5926
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6ffb4c96c42c9a648410ce7f170bffd89d2a713132b62ee5755a10aa496555b
|
|
| MD5 |
6a4d7ced34642bf15042864951d42ef1
|
|
| BLAKE2b-256 |
b495e303f1e5d23dc7e3057142f987594fd986c7287a4a98a5b927e1422541cb
|