-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_eventbus_multiple_handlers.py
More file actions
63 lines (47 loc) · 1.89 KB
/
12_eventbus_multiple_handlers.py
File metadata and controls
63 lines (47 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Multiple Handlers for Same Event
Demonstrates:
- Multiple handlers subscribing to the same event type
- Fan-out pattern where one event triggers multiple handlers
- All handlers execute when event is published
"""
import asyncio
import logging
from opensecflow.eventbus.memory_broker import AsyncQueueBroker
from opensecflow.eventbus.eventbus import EventBus
from opensecflow.eventbus.event import ScopedEvent, EventScope
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
async def main():
"""Multiple handlers for the same event"""
print("\n=== Multiple Handlers for Same Event ===\n")
process_broker = AsyncQueueBroker()
app_broker = AsyncQueueBroker()
class OrderShippedEvent(ScopedEvent):
type: str = "order.shipped"
order_id: str
tracking_number: str
scope: EventScope = EventScope.APP
async def send_notification(event_data: dict):
print(f" 📱 Sending notification for order: {event_data.get('order_id')}")
async def update_inventory(event_data: dict):
print(f" 📦 Updating inventory for order: {event_data.get('order_id')}")
async def log_shipment(event_data: dict):
print(f" 📝 Logging shipment: {event_data.get('tracking_number')}")
async with EventBus(process_broker, app_broker) as bus:
# Register multiple handlers
bus.subscribe("order.shipped", send_notification)
bus.subscribe("order.shipped", update_inventory)
bus.subscribe("order.shipped", log_shipment)
# Publish event - all handlers will be called
event = OrderShippedEvent(
source="shipping-service",
order_id="ORD-002",
tracking_number="TRK-123456"
)
await bus.publish(event)
await asyncio.sleep(0.2)
if __name__ == "__main__":
asyncio.run(main())