File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 11# ZeroMQ基础
2+ ## 一、ZMQ的三种消息模式
3+ ### 1.1. Request-Reply(请求-应答模式)
4+ * 使用Request-Reply模式,需要遵循一定的规律。
5+ * 客户端必须先发送消息,再接收消息;服务端必须先进行接收客户端发送过来的消息,再发送应答给客户端。如此循环。
6+ * 服务端和客户端谁先启动,效果都是一样的。
7+ * 服务端在收到消息之前,会一直阻塞,等待客户端连上来。
8+
9+ #### 创建一个客户端和服务端,客户端发送Hello给服务端,服务端返回World给客户端
10+ #### client.py
11+
12+ import zmq
13+
14+ context = zmq.Context()
15+ socket = context.socket(zmq.REQ)
16+ socket.connect("tcp://localhost:5555")
17+
18+ if __name__ == '__main__':
19+ print('zmq client start....')
20+ for i in range(1, 10):
21+ socket.send_string("hello")
22+ message = socket.recv()
23+ print('received reply message:{}'.format(message))
24+
25+ #### server.py
26+
27+ import zmq
28+ import time
29+
30+ context = zmq.Context()
31+ socket = context.socket(zmq.REP)
32+ socket.bind("tcp://*:5555")
33+ count = 0
34+
35+ if __name__ == '__main__':
36+ print('zmq server start....')
37+ while True:
38+ message = socket.recv()
39+ count += 1
40+ print('received request. message:{} count:{}'.format(message, count))
41+ time.sleep(1)
42+ socket.send_string("World!")
43+
44+ ### 1.2. Publisher-Subscriber(发布-订阅模式)
You can’t perform that action at this time.
0 commit comments