ZeroMQ 推送套接字导致客户端在没有进程正在侦听时终止 [英] ZeroMQ push socket causes client to not terminate when no process is listening

查看:33
本文介绍了ZeroMQ 推送套接字导致客户端在没有进程正在侦听时终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 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) 终止套接字上下文.

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 无限期地等待,直到它能够传递卡住的消息.

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 推送套接字导致客户端在没有进程正在侦听时终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆