-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_rpc_pattern.py
More file actions
60 lines (46 loc) · 1.74 KB
/
06_rpc_pattern.py
File metadata and controls
60 lines (46 loc) · 1.74 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
"""RPC 请求-响应模式示例
演示 RPC(请求-响应)模式的使用。
"""
import asyncio
from opensecflow.eventbus.memory_broker import AsyncQueueBroker
async def main():
"""演示 RPC(请求-响应)模式"""
print("\n=== RPC 请求-响应模式示例 ===\n")
broker = AsyncQueueBroker()
@broker.subscriber("calculator.add")
async def add_service(data: dict) -> dict:
result = data["a"] + data["b"]
print(f" [Service] Computing {data['a']} + {data['b']} = {result}")
return {"result": result}
@broker.subscriber("calculator.multiply")
async def multiply_service(data: dict) -> dict:
result = data["x"] * data["y"]
print(f" [Service] Computing {data['x']} * {data['y']} = {result}")
return {"result": result}
await broker.start()
# 发送 RPC 请求并等待响应
print(" [Client] Sending request: 5 + 3")
response1 = await broker.request(
{"a": 5, "b": 3},
channel="calculator.add",
timeout=1.0
)
print(f" [Client] Got response: {response1['result']}\n")
print(" [Client] Sending request: 7 * 6")
response2 = await broker.request(
{"x": 7, "y": 6},
channel="calculator.multiply",
timeout=1.0
)
print(f" [Client] Got response: {response2['result']}\n")
# 并发发送多个请求
print(" [Client] Sending 3 concurrent requests...")
tasks = [
broker.request({"a": i, "b": i+1}, channel="calculator.add", timeout=1.0)
for i in range(3)
]
responses = await asyncio.gather(*tasks)
print(f" [Client] Got {len(responses)} responses: {[r['result'] for r in responses]}")
await broker.stop()
if __name__ == "__main__":
asyncio.run(main())