python – ZeroMQ推送套接字导致客户端在没有进程正在侦听时不终止

我刚刚开始搞乱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的延迟功能.引自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推送套接字导致客户端在没有进程正在侦听时不终止 - 代码日志