如何解决Python ZeroMQ REP NOBLOCK
我想在 Python ZeroMQ 中使用 NOBLOCK
,pyzmq。
服务器代码:
import zmq
import json
context = zmq.Context()
server = context.socket(zmq.REP)
server.bind("tcp://*:8888")
while True:
try:
buffer = server.recv(flags=zmq.NOBLOCK)
print(json.loads(buffer))
server.send(b'a')
except zmq.ZMQError as e:
pass
客户代码:
import zmq
import json
import time
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:8888")
while True:
data = {
"time": time.asctime(time.localtime(time.time()))
}
print(data)
try:
socket.send_json(json.dumps(data))
socket.recv(flags=zmq.NOBLOCK)
except zmq.ZMQError as e:
pass
我先运行 Server
,然后是客户端。我期望的结果是 Server
和 Client
总是 print time
,但 Server
只打印一次。
有什么问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。