-
- #!/usr/bin/env python
-
- import traceback
- import sys
-
- import zmq
- from time import sleep
-
- context = zmq.Context()
- context2 = zmq.Context()
-
- sender = context.socket(zmq.PUB)
- sender.bind("tcp://*:5557")
-
- receiver = context2.socket(zmq.SUB)
- receiver.connect("tcp://localhost:5557")
- receiver.setsockopt(zmq.SUBSCRIBE, "B")
-
- for i in range(1,10):
- msg = "msg %d" % (i)
- env = None
- if i % 2 == 0:
- env = ["B", msg]
- else:
- env = ["A", msg]
- print "Send Message: ", env
- sender.send_multipart(env)
- sender.close()
-
- timeout = 0
- while timeout < 10:
- try:
- print receiver.recv_multipart(zmq.NOBLOCK)
- except:
- print traceback.format_exception(*sys.exc_info())
- timeout += 1
- sleep(2)
-
- """
- Output:
-
- Send Message: ['A', 'msg 1']
- Send Message: ['B', 'msg 2']
- Send Message: ['A', 'msg 3']
- Send Message: ['B', 'msg 4']
- Send Message: ['A', 'msg 5']
- Send Message: ['B', 'msg 6']
- Send Message: ['A', 'msg 7']
- Send Message: ['B', 'msg 8']
- Send Message: ['A', 'msg 9']
- ['Traceback (most recent call last):\n', ' File "./test.py", line 33, in <module>\n print receiver.recv_multipart(zmq.NOBLOCK)\n', ' File "socket.pyx", line 611, in zmq.core.socket.Socket.recv_multipart (zmq/core/socket.c:5181)\n', ' File "socket.pyx", line 514, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4811)\n', ' File "socket.pyx", line 548, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4673)\n', ' File "socket.pyx", line 99, in zmq.core.socket._recv_copy (zmq/core/socket.c:1344)\n', 'ZMQError: Resource temporarily unavailable\n']
- ['Traceback (most recent call last):\n', ' File "./test.py", line 33, in <module>\n print receiver.recv_multipart(zmq.NOBLOCK)\n', ' File "socket.pyx", line 611, in zmq.core.socket.Socket.recv_multipart (zmq/core/socket.c:5181)\n', ' File "socket.pyx", line 514, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4811)\n', ' File "socket.pyx", line 548, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4673)\n', ' File "socket.pyx", line 99, in zmq.core.socket._recv_copy (zmq/core/socket.c:1344)\n', 'ZMQError: Resource temporarily unavailable\n']
- ['Traceback (most recent call last):\n', ' File "./test.py", line 33, in <module>\n print receiver.recv_multipart(zmq.NOBLOCK)\n', ' File "socket.pyx", line 611, in zmq.core.socket.Socket.recv_multipart (zmq/core/socket.c:5181)\n', ' File "socket.pyx", line 514, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4811)\n', ' File "socket.pyx", line 548, in zmq.core.socket.Socket.recv (zmq/core/socket.c:4673)\n', ' File "socket.pyx", line 99, in zmq.core.socket._recv_copy (zmq/core/socket.c:1344)\n', 'ZMQError: Resource temporarily unavailable\n']
- ^CTraceback (most recent call last):
- File "./test.py", line 37, in <module>
- sleep(2)
- KeyboardInterrupt
- """
-