-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_multiple_subscribers.py
More file actions
39 lines (28 loc) · 1.01 KB
/
02_multiple_subscribers.py
File metadata and controls
39 lines (28 loc) · 1.01 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
"""多订阅者示例
演示多个 handler 订阅同一频道的场景。
"""
import asyncio
from opensecflow.eventbus.memory_broker import AsyncQueueBroker
async def main():
"""多个 handler 订阅同一频道"""
print("\n=== 多订阅者示例 ===\n")
broker = AsyncQueueBroker()
@broker.subscriber("notifications")
async def send_email(data: dict):
print(f" [Email] Sending to {data['email']}")
@broker.subscriber("notifications")
async def send_sms(data: dict):
print(f" [SMS] Sending to {data['phone']}")
@broker.subscriber("notifications")
async def log_notification(data: dict):
print(f" [Log] Notification: {data}")
await broker.start()
await broker.publish(
{"email": "user@example.com", "phone": "138xxxx", "message": "Hello"},
channel="notifications",
)
await asyncio.sleep(0.1)
print(f"\n Subscribers: {broker.get_subscribers()}")
await broker.stop()
if __name__ == "__main__":
asyncio.run(main())