问题描述
我刚刚开始使用 ZeroMQ,我遇到了一个无法正常终止的客户端的问题.特别是我有一个客户端,它可以在没有接收器服务器正在侦听时推送"数据,并且在 python 代码完成后似乎使进程挂起.我假设有一些后台线程需要清理 - 请告诉我如何或指向文档.
I'm just starting to mess with ZeroMQ and I have a problem with a client that doesn't terminate normally. In particular I have a client that may "push" data when no sink server is listening and that seems to make the process hang after the python code has finished. I assume there is some background thread that needs to be cleaned up -- please tell me how or point to documentation.
这是相关的代码段.如果我在没有侦听器的情况下运行进程并取消注释self.push"行,进程会挂起
Here is the relevant piece of code. If I run the process with no listener with the "self.push" line uncommented the process hangs
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() ,但它们似乎没有帮助.
With the line commented (and no listener) the process completes normally.I tried context.term() and context.destroy() and they don't seem to help.
如何清理套接字?或者还有什么线索?提前致谢!
How can I clean up the socket? Or any other clues? Thanks in advance!
推荐答案
这很可能是由于 ZeroMQ 的 linger 功能.引用自 手册页:
This is most probably due to the linger functionality of ZeroMQ. Quoting from the man page:
ZMQ_LINGER 选项应设置指定套接字的延迟时间.延迟周期决定了在使用 zmq_close(3) 关闭套接字后,尚未发送到对等方的未决消息应在内存中停留多长时间,并进一步影响使用 zmq_term(3) 终止套接字上下文.
默认值会导致 ZeroMQ 无限期地等待,直到它能够传递卡住的消息.
The default value causes ZeroMQ to wait indefinitely until it is able to deliver the stuck message.
尝试将 ZMQ_LINGER
套接字选项设置为零或短时间(以毫秒为单位).
Try setting the ZMQ_LINGER
socket option to zero or a short time (in milliseconds).
这篇关于ZeroMQ 推送套接字导致客户端在没有进程正在侦听时终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!