The Python Oracle

Termination of python script while using ZeroMQ with dead server

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC H Dvoks String Quartet No 12 Ame

--

Chapters
00:00 Question
01:41 Accepted answer (Score 8)
02:16 Thank you

--

Full question
https://stackoverflow.com/questions/1714...

Accepted answer links:
[LINGER behaviour]: http://api.zeromq.org/2-1%3azmq-setsocko...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #zeromq #termination #pyzmq

#avk47



ACCEPTED ANSWER

Score 8


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