Created
January 13, 2011 16:56
-
-
Save amacleod/778171 to your computer and use it in GitHub Desktop.
Test case for uninterruptability of zeromq Python runtime.
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
#!/usr/bin/python | |
""" | |
zmq_cli - 0MQ Server interruptability test case, client module. | |
""" | |
import zmq | |
def run(port): | |
context = zmq.Context() | |
socket = context.socket(zmq.REQ) | |
socket.connect("tcp://localhost:%d" % port) | |
print "Sending test." | |
socket.send("test") | |
reply = socket.recv() | |
print "Got reply: %s." % repr(reply) | |
print "Sending EXIT." | |
socket.send("EXIT") | |
reply = socket.recv() | |
print "Got reply: %s." % repr(reply) | |
context.term() | |
if __name__ == "__main__": | |
run(54321) |
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
#!/usr/bin/python | |
""" | |
zmq_serv - 0MQ Server interruptability test case. | |
Symptoms: The server does not stop immediately when I try to interrupt | |
it with ^C. If I subsequently send data to it from a client, the | |
server does stop with a KeyboardInterrupt exception at | |
'self.socket.recv()'. | |
This test case does seem to respect SIGTERM from outside, such as from | |
the 'kill' command. | |
""" | |
import zmq | |
import time | |
import sys | |
class Server(object): | |
""" | |
Simple 0MQ REQ/REP server. It replies to messages, and stops when | |
it gets the "EXIT" message. | |
""" | |
def __init__(self, port): | |
self.port = port | |
self.context = zmq.Context() | |
def run(self): | |
""" | |
Run the REQ/REP server. | |
""" | |
self.socket = self.context.socket(zmq.REP) | |
self.socket.bind("tcp://*:%d" % self.port) | |
print "Listening for messages." | |
while True: | |
message = self.socket.recv() | |
print "Received %d bytes." % len(message) | |
try: | |
reply = self.handle(message) | |
self.socket.send(reply) | |
except Exception as e: | |
self.socket.send("ERROR: %s." % str(e)) | |
def handle(self, message): | |
""" | |
Handle the incoming message. Return an appropriate reply. | |
""" | |
if message == "EXIT": | |
self.stop() | |
return "Hello, %s!" % message | |
def stop(self): | |
""" | |
Stop the server, terminating the 0MQ context. | |
""" | |
self.context.term() | |
time.sleep(1) | |
sys.exit(0) | |
if __name__ == "__main__": | |
s = Server(54321) | |
s.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is test case sample code for https://github.com/zeromq/pyzmq/issues/#issue/64