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
The ZeroMQ Guide has a persistence pattern called Titanic. It's based on a pattern called MajorDomo.
ZeroMQ provides a whole slew of language APIs which run on most operating systems and allows you to communicate seamlessly between all sorts of programs. It also provides a collection of patterns, such as request-reply and publish-subscribe which assist you in creating and structuring your network.
PyZMQ is the Python bindings for ØMQ. This documentation currently contains notes on some important aspects of developing PyZMQ and an overview of what the ØMQ API looks like in Python. For information on how to use ØMQ in general, see the many examples in the excellent ØMQ Guide, all of which have a version in Python.
ZMQ. Context Context is an object serving as a container for all the sockets of a single process. By creating a new context, you start one or more input/output threads: DEFINE context ZMQ.Context. Associated methods: socket()
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With