我刚开始弄乱 ZeroMQ,我遇到了一个无法正常终止的客户端问题。特别是我有一个客户端,当没有接收器服务器正在监听时,它可能会“推送”数据,这似乎会使进程在 python 代码完成后挂起。我假设有一些后台线程需要清理——请告诉我如何清理或指向文档。
这是相关的一段代码。如果我在没有监听器的情况下运行进程,并且“self.push”行未注释,则进程挂起
def setup(self):
print self.name, "connect to sockets"
ctx = self.ctx = zmq.Context()
self.pull = ctx.socket(zmq.PULL)
self.pull.connect(self.ventillatorAddress)
self.push = ctx.socket(zmq.PUSH)
self.push.connect(self.sinkAddress)
self.control = ctx.socket(zmq.SUB)
self.control.connect(self.publisherAddress)
self.control.setsockopt(zmq.SUBSCRIBE, "") # get every control message
self.inbox = ctx.socket(zmq.SUB)
self.inbox.connect(self.distributorAddress)
self.inbox.setsockopt(zmq.SUBSCRIBE, self.name) # listen only for messages addressed with name
def start(self):
print self.name, "push worker is ready signal"
# listen for "go" signal
pollcount = 0
go = False
while not go:
#print "send ready for", self.name
#self.push.send(self.name+" ready")
print "listen for 'go'"
msg = self.recvPoll(self.control)
if msg is None:
pollcount += 1
assert pollcount<10
print "poll timeout", pollcount
time.sleep(1)
continue
pollcount = 0
print "recv'd", msg
assert msg=="go!"
go = True
print "go signal received"
pass
注释行(没有监听器)过程正常完成。 我尝试了 context.term() 和 context.destroy(),但它们似乎没有帮助。
如何清理套接字?或者还有什么线索?提前致谢!
最佳答案
这很可能是由于 ZeroMQ 的 linger 功能。引用自 man page :
The ZMQ_LINGER option shall set the linger period for the specified socket. The linger period determines how long pending messages which have yet to be sent to a peer shall linger in memory after a socket is closed with zmq_close(3), and further affects the termination of the socket's context with zmq_term(3).
默认值导致 ZeroMQ 无限期等待,直到它能够传递卡住的消息。
尝试将 ZMQ_LINGER
套接字选项设置为零或较短的时间(以毫秒为单位)。
关于python - ZeroMQ 推送套接字导致客户端在没有进程监听时不终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040208/