onesecondtrader 0.49.0__py3-none-any.whl → 0.50.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ """
2
+ Provides the infrastructure for event-based communication between system components.
3
+ """
4
+
5
+ from .eventbus import EventBus
6
+ from .subscriber import Subscriber
7
+
8
+ __all__ = [
9
+ "EventBus",
10
+ "Subscriber",
11
+ ]
@@ -0,0 +1,96 @@
1
+ from __future__ import annotations
2
+
3
+ import collections
4
+ import threading
5
+ import typing
6
+
7
+ from onesecondtrader import events
8
+
9
+ if typing.TYPE_CHECKING:
10
+ from .subscriber import Subscriber
11
+
12
+
13
+ class EventBus:
14
+ """
15
+ Event dispatch mechanism for propagating event objects to subscribers.
16
+
17
+ The event bus maintains subscriptions between subscribers and concrete event types.
18
+ Events published to the bus are synchronously delivered to all subscribers registered for the exact event type.
19
+
20
+ Subscription management and event publication are thread-safe.
21
+ Event delivery itself occurs outside the internal lock.
22
+ """
23
+
24
+ def __init__(self) -> None:
25
+ """
26
+ Initialize an empty event bus.
27
+
28
+ The bus starts with no registered subscribers and no active subscriptions.
29
+ """
30
+ self._per_event_subscriptions: collections.defaultdict[
31
+ type[events.EventBase], set[Subscriber]
32
+ ] = collections.defaultdict(set)
33
+ self._subscribers: set[Subscriber] = set()
34
+ self._lock: threading.Lock = threading.Lock()
35
+
36
+ def subscribe(
37
+ self,
38
+ subscriber: Subscriber,
39
+ event_type: type[events.EventBase],
40
+ ) -> None:
41
+ """
42
+ Register a subscriber for a specific event type.
43
+
44
+ The subscriber will receive all future events whose concrete type matches `event_type`.
45
+
46
+ Parameters:
47
+ subscriber:
48
+ Object receiving published events.
49
+ event_type:
50
+ Concrete event class the subscriber is interested in.
51
+ """
52
+ with self._lock:
53
+ self._subscribers.add(subscriber)
54
+ self._per_event_subscriptions[event_type].add(subscriber)
55
+
56
+ def unsubscribe(self, subscriber: Subscriber) -> None:
57
+ """
58
+ Remove a subscriber from all event subscriptions.
59
+
60
+ After unsubscription, the subscriber will no longer receive any events published on this bus.
61
+
62
+ Parameters:
63
+ subscriber:
64
+ Subscriber to remove.
65
+ """
66
+ with self._lock:
67
+ for set_of_event_subscribers in self._per_event_subscriptions.values():
68
+ set_of_event_subscribers.discard(subscriber)
69
+ self._subscribers.discard(subscriber)
70
+
71
+ def publish(self, event: events.EventBase) -> None:
72
+ """
73
+ Publish an event to all subscribed listeners.
74
+
75
+ Subscribers are matched strictly by the concrete type of the event.
76
+ Parent classes and inheritance relationships are not considered.
77
+
78
+ Parameters:
79
+ event:
80
+ Event instance to dispatch.
81
+ """
82
+ with self._lock:
83
+ subscribers = self._per_event_subscriptions[type(event)].copy()
84
+ for subscriber in subscribers:
85
+ subscriber.receive(event)
86
+
87
+ def wait_until_system_idle(self) -> None:
88
+ """
89
+ Block until all subscribers report an idle state.
90
+
91
+ This method delegates to each subscriber's `wait_until_idle` method and returns only after all subscribers have completed any pending work.
92
+ """
93
+ with self._lock:
94
+ subscribers = self._subscribers.copy()
95
+ for subscriber in subscribers:
96
+ subscriber.wait_until_idle()
@@ -0,0 +1,153 @@
1
+ import abc
2
+ import queue
3
+ import threading
4
+
5
+ from onesecondtrader import events, messaging
6
+
7
+
8
+ class Subscriber(abc.ABC):
9
+ """
10
+ Abstract base class for event bus subscribers.
11
+
12
+ A subscriber receives events from an event bus and processes them asynchronously in a dedicated worker thread.
13
+ Incoming events are queued and handled sequentially.
14
+
15
+ Subclasses implement `_on_event` to define event-specific behavior.
16
+ """
17
+
18
+ def __init__(self, event_bus: messaging.EventBus) -> None:
19
+ """
20
+ Initialize the subscriber and start its event-processing thread.
21
+
22
+ Parameters:
23
+ event_bus:
24
+ Event bus used for subscribing to and publishing events.
25
+ """
26
+ self._event_bus = event_bus
27
+ self._queue: queue.Queue[events.EventBase | None] = queue.Queue()
28
+
29
+ self._running: threading.Event = threading.Event()
30
+ self._running.set()
31
+
32
+ self._thread = threading.Thread(
33
+ target=self._event_loop, name=self.__class__.__name__
34
+ )
35
+ self._thread.start()
36
+
37
+ def receive(self, event: events.EventBase) -> None:
38
+ """
39
+ Receive an event from the event bus.
40
+
41
+ The event is enqueued for asynchronous processing if the subscriber is running.
42
+
43
+ Parameters:
44
+ event:
45
+ Event instance delivered by the event bus.
46
+ """
47
+ if self._running.is_set():
48
+ self._queue.put(event)
49
+
50
+ def wait_until_idle(self) -> None:
51
+ """
52
+ Block until all queued events have been processed.
53
+
54
+ If the subscriber is not running, this method returns immediately.
55
+ """
56
+ if not self._running.is_set():
57
+ return
58
+
59
+ self._queue.join()
60
+
61
+ def shutdown(self) -> None:
62
+ """
63
+ Shut down the subscriber and stop event processing.
64
+
65
+ The subscriber is unsubscribed from the event bus, its worker thread is signaled to terminate, and all pending events are processed before shutdown completes.
66
+ """
67
+ if not self._running.is_set():
68
+ return
69
+
70
+ self._event_bus.unsubscribe(self)
71
+ self._running.clear()
72
+ self._queue.put(None)
73
+
74
+ if threading.current_thread() is not self._thread:
75
+ self._thread.join()
76
+
77
+ def _subscribe(self, *event_types: type[events.EventBase]) -> None:
78
+ """
79
+ Subscribe this subscriber to one or more event types.
80
+
81
+ Parameters:
82
+ *event_types:
83
+ Concrete event classes to subscribe to.
84
+ """
85
+ for event_type in event_types:
86
+ self._event_bus.subscribe(self, event_type)
87
+
88
+ def _publish(self, event: events.EventBase) -> None:
89
+ """
90
+ Publish an event to the event bus.
91
+
92
+ Parameters:
93
+ event:
94
+ Event instance to publish.
95
+ """
96
+ self._event_bus.publish(event)
97
+
98
+ def _event_loop(self) -> None:
99
+ """
100
+ Internal worker loop for processing queued events.
101
+
102
+ This method runs in a dedicated thread and should not be called directly.
103
+ """
104
+ while True:
105
+ event = self._queue.get()
106
+
107
+ if event is None:
108
+ self._queue.task_done()
109
+ break
110
+
111
+ try:
112
+ self._on_event(event)
113
+ except Exception as exc:
114
+ self._on_exception(exc)
115
+ finally:
116
+ self._queue.task_done()
117
+
118
+ self._cleanup()
119
+
120
+ def _on_exception(self, exc: Exception) -> None:
121
+ """
122
+ Handle an exception raised during event processing.
123
+
124
+ Subclasses may override this method to implement logging or recovery behavior.
125
+ The default implementation ignores the exception.
126
+
127
+ Parameters:
128
+ exc:
129
+ Exception raised while processing an event.
130
+ """
131
+ pass
132
+
133
+ def _cleanup(self) -> None:
134
+ """
135
+ Perform cleanup after the event loop terminates.
136
+
137
+ Subclasses may override this method to release resources or emit shutdown notifications.
138
+ """
139
+ pass
140
+
141
+ @abc.abstractmethod
142
+ def _on_event(self, event: events.EventBase) -> None:
143
+ """
144
+ Handle a single event.
145
+
146
+ This method is invoked sequentially for each event received by the subscriber.
147
+ Implementations must not block indefinitely, as `wait_until_idle` relies on timely completion.
148
+
149
+ Parameters:
150
+ event:
151
+ Event instance to handle.
152
+ """
153
+ ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: onesecondtrader
3
- Version: 0.49.0
3
+ Version: 0.50.0
4
4
  Summary: The Trading Infrastructure Toolkit for Python. Research, simulate, and deploy algorithmic trading strategies — all in one place.
5
5
  License-File: LICENSE
6
6
  Author: Nils P. Kujath
@@ -22,13 +22,16 @@ onesecondtrader/indicators/__init__.py,sha256=i-syRqCdQdC2BLL-7bDBcdl2Mll1LgE7hi
22
22
  onesecondtrader/indicators/base.py,sha256=iGfgOj1B_LRGBeh7VjM5OjxoYnSxV7JZUP1C3O_dfmE,4653
23
23
  onesecondtrader/indicators/market_fields.py,sha256=Znyii0egBkJT3CFlYhQ5ArRNidmBLF0TzgCr2AWu5CA,4306
24
24
  onesecondtrader/indicators/moving_averages.py,sha256=Ej3Vg-K4Kf93J3MS6av2J8FRP1Kqx4G6yNZj1QfE9lU,3411
25
+ onesecondtrader/messaging/__init__.py,sha256=VIVxQmJR3E50AuAze50CHIygRHJSwg26fC3ZBkiUnbQ,209
26
+ onesecondtrader/messaging/eventbus.py,sha256=l010Sh57ti9y-R1E8fa597YmoC5c40vR_oyu0eTKEjg,3234
27
+ onesecondtrader/messaging/subscriber.py,sha256=KDtUNipe0dXl05ClIeQevIK2V7u5A4O-P84kpfS_Y28,4479
25
28
  onesecondtrader/models/__init__.py,sha256=XWL6aNLwAA2JQMoqK2PY-_CwigV0ighx4zwGQVdmtCs,529
26
29
  onesecondtrader/models/bar_fields.py,sha256=GnLBL08ueUr35w2dAbKwOBWrdBS98OC9r0T2NifwTH8,646
27
30
  onesecondtrader/models/bar_period.py,sha256=J8ncVtcAxR52uD0nbC8Knds_GUP5wiuNj5rAKq4vv-4,475
28
31
  onesecondtrader/models/order_types.py,sha256=SiJamarLQ7zkHzHLLbd86I_TeZrQJ4QEIMqNHj4dxXU,737
29
32
  onesecondtrader/models/rejection_reasons.py,sha256=BToLmPholsP9GcLGZ874nQJpehZ1yB1SFyYqlf3AOwc,2127
30
33
  onesecondtrader/models/trade_sides.py,sha256=Pf9BpxoUxqgKC_EKAExfSqgfIIK9NW-RpJES0XHRF-8,583
31
- onesecondtrader-0.49.0.dist-info/METADATA,sha256=EFqtCI8r8EWYjn-l1LOkaWIsF0QrUZyq1UFA2WI56Dk,9951
32
- onesecondtrader-0.49.0.dist-info/WHEEL,sha256=3ny-bZhpXrU6vSQ1UPG34FoxZBp3lVcvK0LkgUz6VLk,88
33
- onesecondtrader-0.49.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
34
- onesecondtrader-0.49.0.dist-info/RECORD,,
34
+ onesecondtrader-0.50.0.dist-info/METADATA,sha256=osrTVFj2ZqYZwPeBnaMBkRnfbfZaBIcl9YDpBhyrD-U,9951
35
+ onesecondtrader-0.50.0.dist-info/WHEEL,sha256=3ny-bZhpXrU6vSQ1UPG34FoxZBp3lVcvK0LkgUz6VLk,88
36
+ onesecondtrader-0.50.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
37
+ onesecondtrader-0.50.0.dist-info/RECORD,,