Skip to content

bus

The event bus used for telemetry across the system.

Defines Subscription and EventBus. Publishers emit TelemetryRecords; subscribers register a topic string (with "*" matching every record), an optional filter, and a handler. A record is dispatched to a subscription when its topic relates to the record's kind (see EventBus.process_event for the exact comparison). While no subscribers exist, published records are queued (up to a maximum) for later delivery via process_unprocessed_events.

EventBus(maximum_queue_size=500, subscribers=list(), unprocessed_events=list()) dataclass

Represents an event bus for communication between different parts of the system.

Attributes:

Name Type Description
subscribers list[Subscription]

The list of subscribers to the bus.

unprocessed_events list[TelemetryRecord]

The list of unprocessed events. When there are no subscribers, events are queued here.

maximum_queue_size int

The maximum size of the event queue.

process_event(event, subscription)

Process a record for a given subscription.

Parameters:

Name Type Description Default
event TelemetryRecord

The telemetry record to process.

required
subscription Subscription

The subscription to process the record for.

required

process_unprocessed_events()

Deliver queued events to the current subscribers.

Runs every queued event through every subscription (subject to the usual topic and filter checks). The queue is not cleared, so events remain available for subscribers added later.

publish(event)

Publish a record to the bus.

Parameters:

Name Type Description Default
event TelemetryRecord

The telemetry record to publish.

required

subscribe(topic, handler, filter=None, once=False)

Subscribe a handler to a topic.

Parameters:

Name Type Description Default
topic str

The topic to subscribe to.

required
handler Callable[[TelemetryRecord], None]

The handler to call when an event is published to the topic.

required
filter Callable | None

An optional filter function to filter events.

None
once bool

Whether to only handle one event.

False

Returns:

Type Description
Subscription

The subscription object.

unsubscribe(subscription)

Unsubscribe a handler from the bus.

Parameters:

Name Type Description Default
subscription Subscription

The subscription to remove.

required

Subscription(topic, handler, filter=None, once=False) dataclass

Represents a subscription to a kind of record on the bus.

Attributes:

Name Type Description
topic str

The record kind to which the subscription is made ("*" for all).

handler Callable[[TelemetryRecord], None]

The handler function to be called when a record is published to the topic.

filter Callable | None

An optional filter function to filter records.

once bool

Whether the subscription should be removed after the first record.