Aaron Watters
Aaron Watters

Reputation: 2846

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

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!

Upvotes: 4

Views: 1419

Answers (1)

Mika Fischer
Mika Fischer

Reputation: 4268

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).

Upvotes: 6

Related Questions