summaryrefslogtreecommitdiffstats
path: root/external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py
diff options
context:
space:
mode:
Diffstat (limited to 'external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py')
-rw-r--r--external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py b/external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py
new file mode 100644
index 00000000..25da5d90
--- /dev/null
+++ b/external_libs/python/pyzmq-14.7.0/examples/win32-interrupt/display.py
@@ -0,0 +1,45 @@
+"""The display part of a simply two process chat app."""
+
+# This file has been placed in the public domain.
+
+
+import zmq
+from zmq.utils.win32 import allow_interrupt
+
+
+def main(addrs):
+ context = zmq.Context()
+ control = context.socket(zmq.PUB)
+ control.bind('inproc://control')
+ updates = context.socket(zmq.SUB)
+ updates.setsockopt(zmq.SUBSCRIBE, "")
+ updates.connect('inproc://control')
+ for addr in addrs:
+ print "Connecting to: ", addr
+ updates.connect(addr)
+
+ def interrupt_polling():
+ """Fix CTRL-C on Windows using "self pipe trick"."""
+ control.send_multipart(['', 'quit'])
+
+ with allow_interrupt(interrupt_polling):
+ message = ''
+ while message != 'quit':
+ message = updates.recv_multipart()
+ if len(message) < 2:
+ print 'Invalid message.'
+ continue
+ account = message[0]
+ message = ' '.join(message[1:])
+ if message == 'quit':
+ print 'Killed by "%s".' % account
+ break
+ print '%s: %s' % (account, message)
+
+
+if __name__ == '__main__':
+ import sys
+ if len(sys.argv) < 2:
+ print "usage: display.py <address> [,<address>...]"
+ raise SystemExit
+ main(sys.argv[1:])