7

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
4

1 回答 1

8

您遇到了 ZeroMQ 的LINGER 行为。LINGER 定义了 Context 在允许Context.term丢弃消息之前应该等待多长时间。ZeroMQ 2.x 中的默认值为永远,ZeroMQ 3.x 中的默认值为一秒。如果你告诉你的套接字它们应该只停留很短的时间,你的脚本应该可以正常退出:

socket = context.socket(zmq.REQ)
socket_id = randomID()
socket.identity = ramdomID()
socket.linger = 250 # 250ms = 1/4 s
socket.connect("tcp://dead_server")
于 2013-06-17T21:12:10.673 回答