ZeroMQ push socket causes client to not terminate when no process is listening

自闭症网瘾萝莉.ら 提交于 2019-12-22 08:17:01

问题


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.

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

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!


回答1:


This is most probably due to the linger functionality of ZeroMQ. Quoting from the 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).

The default value causes ZeroMQ to wait indefinitely until it is able to deliver the stuck message.

Try setting the ZMQ_LINGER socket option to zero or a short time (in milliseconds).



来源:https://stackoverflow.com/questions/9040208/zeromq-push-socket-causes-client-to-not-terminate-when-no-process-is-listening

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!