Reputation: 85
I currently use ZeroMQ with Java binding. My program is in a PUB/SUB
mode.
It seems reasonable to set a timeout, while a client can't receive a message from PUB
-side.
But for the publish server, who sends messages without a fixed frequency, it's hard to decide a reasonable timeout.
On the other hand, if no timeout is set, then a program would probably stuck at function:
recv()
forever even publish server is dead.
If there a good solution to fix this issue?
Upvotes: 1
Views: 1290
Reputation: 1
A principally best solution is to use a Poller
instance, for which a .poll()
method tells your code, again with a help of an explicitly configurable aTimeOut
in [ms], whether there is any incoming message to either attempt a non-blocking .recv()
method or not even trying to do so, once there is NACK for any such message being present from the call to a .poll()
method ( for details check the API ).
Another way is to use a non-blocking mode of a call to the .recv( aSockINST, ZMQ_DONTWAIT )
method. Here, the API + wrapper / binding, specify how it handles a state, where none such message was locally ready to get .recv()
-ed, so that one may rely on the common language's available syntax-scaffolding - like { try: except: finally: }
or { try: catch: }
or { defer; panic(); recover() }
- to handle either of the return states from the .recv( .., ZMQ_DONTWAIT )
call. Similar rules apply to an ( almost ) blocking call, with some moderately small .recv()
timeout.
Upvotes: 1
Reputation: 1716
You can use Pollers:
poller = zmq.Poller()
poller.register(client_receiver, zmq.POLLIN);
for further reading: http://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/multisocket/zmqpoller.html
hope it helps.
Upvotes: 1