
English | Türkçe
FlowEvent
Repository · Changelog · Latest release · MIT License
In-process event delivery for Python applications and libraries. Use a synchronous or async bus to publish typed events and manage subscriptions without a message broker.
FlowEvent began as the event subsystem of Vehicle Autonomy Core.
Contents
- Getting started
- Core concepts
- Working with events
- Async applications
- Performance and the C extension
- Development and license
Getting started
Install
Install from the project root with Python 3.10 or later.
python -m pip install flowevent
Publish your first event
A publisher hands an ordinary Python object to the bus. The subscriber receives it before the synchronous publish returns. Leaving the inner context cancels the subscription.
from dataclasses import dataclass
from flowevent import EventBus
@dataclass(frozen=True)
class OrderPlaced:
order_id: str
def send_confirmation(event: OrderPlaced) -> None:
print(f"Order placed: {event.order_id}")
with EventBus[OrderPlaced]() as bus:
with bus.subscribe(send_confirmation):
result = bus.publish(OrderPlaced("order-42"))
assert result.delivered == 1
Core concepts
Choose EventBus for synchronous handlers and AsyncEventBus for coroutines. Use named channels when event groups have separate ownership. AsyncEventQueue connects a synchronous publisher to an async consumer. Delivery stays inside one process. Type hints describe the expected event to your editor and type checker. Apply a filter when you need to select events at runtime.
Working with events
Filter and replay recent events
Give the bus a bounded history, then subscribe with a routing key. The example replays the latest matching event and receives subsequent live matches using the same subscription.
from flowevent import EventBus
received = []
with EventBus[tuple[str, str]](history=3, routing_key=lambda event: event[0]) as bus:
bus.publish(("orders", "created"))
bus.publish(("billing", "invoice_sent"))
with bus.subscribe(received.append, routing_key="orders", replay=1):
bus.publish(("orders", "packed"))
assert received == [("orders", "created"), ("orders", "packed")]
Async applications
Publish to coroutine handlers
Use AsyncEventBus when handlers already perform async work. Keep the bus and its subscriptions on the event loop that owns them.
If you opt into DeliveryMode.CONCURRENT, set max_concurrency to bound the number of handlers run concurrently by each publish. Without that limit, a publish can create a task for every matching handler.
import asyncio
from flowevent import AsyncEventBus
async def main() -> None:
received = []
async def collect(event: str) -> None:
received.append(event)
async with AsyncEventBus[str]() as bus:
subscription = await bus.subscribe(collect)
async with subscription:
await bus.publish("order.shipped")
assert received == ["order.shipped"]
asyncio.run(main())
Performance and the C extension
The optional C core reduces dispatch overhead for supported synchronous EventBus paths. This matters most when publishing frequently or routing to many subscribers with small handlers. The pure-Python implementation provides the same API. See the benchmark archive for workloads, environments, and measurements from published releases.
Development and license
python -m pip install -e ".[dev]"
python run_tests.py
Run python run_tests.py --native to rebuild and require the C extension for the tests.
This project is licensed under the MIT License.
Related projects
FlowCoord · FlowPipeline