spacepaste

  1.  
  2. #!/usr/bin/env python
  3. import traceback
  4. import sys
  5. import zmq
  6. from time import sleep
  7. print "Creating the zmq.Context"
  8. context = zmq.Context()
  9. print "Binding the publisher to the local socket at port 5557"
  10. sender = context.socket(zmq.PUB)
  11. sender.bind("tcp://*:5557")
  12. print "Binding the subscriber to the local socket at port 5557"
  13. receiver = context.socket(zmq.SUB)
  14. receiver.connect("tcp://*:5557")
  15. print "Setting the subscriber option to get only those originating from \"B\""
  16. receiver.setsockopt(zmq.SUBSCRIBE, "B")
  17. print "Waiting a second for the socket to be created."
  18. sleep(1)
  19. print "Sending messages"
  20. for i in range(1,10):
  21. msg = "msg %d" % (i)
  22. env = None
  23. if i % 2 == 0:
  24. env = ["B", msg]
  25. else:
  26. env = ["A", msg]
  27. print "Sending Message: ", env
  28. sender.send_multipart(env)
  29. print "Closing the sender."
  30. sender.close()
  31. failed_attempts = 0
  32. while failed_attempts < 3:
  33. try:
  34. print str(receiver.recv_multipart(zmq.NOBLOCK))
  35. except:
  36. print traceback.format_exception(*sys.exc_info())
  37. failed_attempts += 1
  38. print "Closing the receiver."
  39. receiver.close()
  40. print "Terminating the context."
  41. context.term()
  42. """
  43. Output:
  44. Creating the zmq.Context
  45. Binding the publisher to the local socket at port 5557
  46. Binding the subscriber to the local socket at port 5557
  47. Setting the subscriber option to get only those originating from "B"
  48. Waiting a second for the socket to be created.
  49. Sending messages
  50. Sending Message: ['A', 'msg 1']
  51. Sending Message: ['B', 'msg 2']
  52. Sending Message: ['A', 'msg 3']
  53. Sending Message: ['B', 'msg 4']
  54. Sending Message: ['A', 'msg 5']
  55. Sending Message: ['B', 'msg 6']
  56. Sending Message: ['A', 'msg 7']
  57. Sending Message: ['B', 'msg 8']
  58. Sending Message: ['A', 'msg 9']
  59. Closing the sender.
  60. ['B', 'msg 2']
  61. ['B', 'msg 4']
  62. ['B', 'msg 6']
  63. ['B', 'msg 8']
  64. ['Traceback (most recent call last):\n', ' File "./test.py", line 43, in <module>\n print str(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']
  65. ['Traceback (most recent call last):\n', ' File "./test.py", line 43, in <module>\n print str(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']
  66. ['Traceback (most recent call last):\n', ' File "./test.py", line 43, in <module>\n print str(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']
  67. Closing the receiver.
  68. Terminating the context.
  69. """
  70.