【发布时间】:2019-12-30 20:15:33
【问题描述】:
由于我的工作原因,使用 ZeroMQ REQ/REP 模式,我决定让服务器的回复器在与主体不同的线程中工作。我将展示的代码总结了这种方法:
import time
import zmq
import threading
def make_work(context):
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
message = socket.recv()
print("Received request: %s" % message)
#Do some 'work'
time.sleep(1)
#Send reply back to client
socket.send(b"World")
socket.close()
context = zmq.Context()
thr = None
while True:
if not thr or not thr.is_alive():
thr = threading.Thread(target = make_work, args = (context, ) )
thr.start()
我修改了 pyzmq 指南的 hello world 示例。所以,我的问题是,当我从 Pieter Hintjens 制作的文档运行 hello world 客户端时,预期的行为是:对于我正在创建的每个线程,我打开的应答器套接字将答案发送给客户端,但是真正的行为是,在第一个线程之后,连接块的两侧。如果我在客户端进行投票,然后重试发送,那就是成功,但这不是我想要的。在服务器端,在新线程中是否有可能成功接收?
【问题讨论】:
标签: python multithreading networking zeromq