Created
October 29, 2011 17:34
-
-
Save carlopires/1324829 to your computer and use it in GitHub Desktop.
Right way to connect with mongrel2 server REQ/REP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FROM: https://github.com/zeromq/pyzmq/issues/132 | |
# | |
# You need to set LINGER to something other than -1 (the default) if | |
# you want the socket to stop trying to send. The gist is that you | |
# should be able to do: | |
# | |
# socket.send('foo') | |
# socket.close() | |
# | |
# and close() will block until the socket is done sending messages. | |
# Since your peer never arrives, the socket keeps waiting. LINGER | |
# is the amount of time (in ms) that it will block. The default is | |
# -1, or forever. setsockopt(LINGER, 0) means close() will drop | |
# everything and return right away. | |
import zmq | |
INVALID_ADDR = 'ipc:///tmp/idontexist.socket' | |
context = zmq.Context() | |
socket = context.socket(zmq.REQ) | |
# zmq.LINGER is timeout of zmq.send() | |
socket.setsockopt(zmq.LINGER, 0) | |
socket.connect(INVALID_ADDR) | |
try: | |
socket.send('hello') | |
poller = zmq.Poller() | |
poller.register(socket, zmq.POLLIN) | |
conn = dict(poller.poll(1000)) | |
if conn: | |
if conn.get(socket) == zmq.POLLIN: | |
print "got result: ", socket.recv(zmq.NOBLOCK) | |
else: | |
print 'got no result' | |
finally: | |
socket.close() | |
context.term() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment