Subscriber
Subscriber
Bases: ABC
Abstract base class for event bus subscribers.
A subscriber receives events from an event bus and processes them asynchronously in a dedicated worker thread. Incoming events are queued and handled sequentially.
Subclasses implement _on_event to define event-specific behavior.
__init__(event_bus)
Initialize the subscriber and start its event-processing thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_bus
|
EventBus
|
Event bus used for subscribing to and publishing events. |
required |
Source code in src/onesecondtrader/messaging/subscriber.py
receive(event)
Receive an event from the event bus.
The event is enqueued for asynchronous processing if the subscriber is running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EventBase
|
Event instance delivered by the event bus. |
required |
Source code in src/onesecondtrader/messaging/subscriber.py
wait_until_idle()
Block until all queued events have been processed.
If the subscriber is not running, this method returns immediately.
Source code in src/onesecondtrader/messaging/subscriber.py
shutdown()
Shut down the subscriber and stop event processing.
The subscriber is unsubscribed from the event bus, its worker thread is signaled to terminate, and all pending events are processed before shutdown completes.
Source code in src/onesecondtrader/messaging/subscriber.py
_subscribe(*event_types)
Subscribe this subscriber to one or more event types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*event_types
|
type[EventBase]
|
Concrete event classes to subscribe to. |
()
|
Source code in src/onesecondtrader/messaging/subscriber.py
_publish(event)
Publish an event to the event bus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EventBase
|
Event instance to publish. |
required |
_event_loop()
Internal worker loop for processing queued events.
This method runs in a dedicated thread and should not be called directly.
Source code in src/onesecondtrader/messaging/subscriber.py
_on_exception(exc)
Handle an exception raised during event processing.
Subclasses may override this method to implement logging or recovery behavior. The default implementation ignores the exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
Exception
|
Exception raised while processing an event. |
required |
Source code in src/onesecondtrader/messaging/subscriber.py
_cleanup()
Perform cleanup after the event loop terminates.
Subclasses may override this method to release resources or emit shutdown notifications.
_on_event(event)
abstractmethod
Handle a single event.
This method is invoked sequentially for each event received by the subscriber.
Implementations must not block indefinitely, as wait_until_idle relies on timely completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EventBase
|
Event instance to handle. |
required |