-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_eventbus_context_manager.py
More file actions
56 lines (41 loc) · 1.43 KB
/
11_eventbus_context_manager.py
File metadata and controls
56 lines (41 loc) · 1.43 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
"""EventBus with Context Manager
Demonstrates:
- Using async with EventBus() for automatic lifecycle management
- Clean resource management pattern
- Automatic start/stop handling
"""
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():
"""Using async with for automatic lifecycle management"""
print("\n=== EventBus with Context Manager ===\n")
process_broker = AsyncQueueBroker()
app_broker = AsyncQueueBroker()
class PaymentEvent(ScopedEvent):
type: str = "payment.completed"
payment_id: str
amount: float
scope: EventScope = EventScope.APP
async def handle_payment(event_data: dict):
print(f" 💰 Payment processed: {event_data}")
# Use context manager
async with EventBus(process_broker, app_broker) as bus:
bus.subscribe("payment.completed", handle_payment)
event = PaymentEvent(
source="payment-service",
payment_id="PAY-001",
amount=199.99
)
await bus.publish(event)
await asyncio.sleep(0.1)
print(" EventBus stopped automatically")
if __name__ == "__main__":
asyncio.run(main())