Question

I have a problem in closing python application while I using ZeroMQ. first I connect to a server that is not running!

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket_id = randomID()
socket.setsockopt(zmq.IDENTITY, socket_id)
socket.connect("tcp://dead_server")
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)

and sending my message

socket.send(msg)

waiting for reply

sockets = dict(poller.poll(1000))

if sockets.get(socket) == zmq.POLLIN:
    result = socket.recv()
    print (result)

so the server is dead the message will be not send and there is no reply. it is work true. then I close socket and unregister it from poller then connect to alive server with new socket and I send message by the socket and get reply from it.

poller.unregister(socket)
socket.close()

socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.IDENTITY, socket_id)
poller.register(socket, zmq.POLLIN)
socket.connect("tcp://alive_server")
socket.send(msg)

sockets = dict(poller.poll(1000))

if sockets.get(socket) == zmq.POLLIN:
    result = socket.recv()
    print (result)

# Every thing ok up to hear

after it the application (python script) is not closed (terminate)! it is my problem. [I can close application with sig_term but i won't to use it for some reason.] if I don't send first message to dead server the application is closed truly. I guess the problem is ZeroMQ io thread but I can't solve it.

exit(0)    # Not worked
sys.exit(0)    # Not worked
Was it helpful?

Solution

You are encountering ZeroMQ's LINGER behaviour. LINGER defines how long the Context should wait before allowing Context.term to discard messages. The default in ZeroMQ 2.x is forever, and the default in ZeroMQ 3.x is one second. If you tell your sockets that they should only linger a short time, your script should exit just fine:

socket = context.socket(zmq.REQ)
socket_id = randomID()
socket.identity = ramdomID()
socket.linger = 250 # 250ms = 1/4 s
socket.connect("tcp://dead_server")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top