-
-
Save benthomasson/7794e65ccaba0a53b612e8ba8b54de1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from __future__ import print_function | |
| from nanomsg import Socket, REQ, REP, PUB, SUB, DONTWAIT, \ | |
| NanoMsgAPIError, EAGAIN, SUB_SUBSCRIBE | |
| import time | |
| from gevent import monkey | |
| import gevent | |
| monkey.patch_all() | |
| print("REQ/REP Sample") | |
| s1 = Socket(REP) | |
| s2 = Socket(REQ) | |
| try: | |
| s1.bind('inproc://hello') | |
| s2.connect('inproc://hello') | |
| s2.send(b'hello nanomsg', flags=DONTWAIT) | |
| msg = s1.recv() | |
| print("server got: " + msg) | |
| s1.send(b'hello back', flags=DONTWAIT) | |
| print("client got: " + s2.recv()) | |
| finally: | |
| s1.close() | |
| s2.close() | |
| def send(socket, msg): | |
| print("Sending message: %s" % msg) | |
| socket.send(msg, flags=DONTWAIT) | |
| print("\nPUB/SUB Sample") | |
| s1 = Socket(PUB) | |
| s2 = Socket(SUB) | |
| try: | |
| s1.bind('inproc://hellopubsub') | |
| s2.connect('inproc://hellopubsub') | |
| s2.set_string_option(SUB, SUB_SUBSCRIBE, "") #subscribe to all topics | |
| print("Sending messages later") | |
| gevent.spawn_later(1, send, s1, 'hello nano 1') | |
| gevent.spawn_later(2, send, s1, 'hello nano 2') | |
| gevent.spawn_later(3, send, s1, 'hello nano 3') | |
| gevent.spawn_later(4, send, s1, 'hello nano 4') | |
| gevent.spawn_later(5, send, s1, 'hello nano 5') | |
| print("receiving message") | |
| count = 0 | |
| while True: | |
| try: | |
| msg = s2.recv(flags=DONTWAIT) | |
| print("client got: " + msg) | |
| count += 1 | |
| if count >= 5: | |
| break | |
| except NanoMsgAPIError, ne: | |
| if ne.errno != EAGAIN: | |
| raise | |
| print('timeout') | |
| #else no message available | |
| gevent.sleep(0.5) | |
| finally: | |
| s1.close() | |
| s2.close() | |
| print("\nPUB/SUB Sample with Subscription") | |
| s1 = Socket(PUB) | |
| s2 = Socket(SUB) | |
| try: | |
| s1.bind('inproc://hellopubsub') | |
| s2.connect('inproc://hellopubsub') | |
| #subscribe to hello messages (starting with the 'hello' prefix) | |
| s2.set_string_option(SUB, SUB_SUBSCRIBE, "hello") | |
| print("Sending messages later") | |
| gevent.spawn_later(1, send, s1, 'hello nano 1') | |
| gevent.spawn_later(2, send, s1, 'something else') | |
| gevent.spawn_later(3, send, s1, 'hello nano 2') | |
| gevent.spawn_later(4, send, s1, 'hello nano 3') | |
| gevent.spawn_later(5, send, s1, 'hello nano 4 ') | |
| print("receiving message") | |
| count = 0 | |
| while True: | |
| try: | |
| msg = s2.recv(flags=DONTWAIT) | |
| print("client got: " + msg) | |
| count += 1 | |
| if count >= 4: | |
| break | |
| except NanoMsgAPIError, ne: | |
| if ne.errno != EAGAIN: | |
| raise | |
| print('timeout') | |
| #else no message available | |
| gevent.sleep(0.5) | |
| finally: | |
| s1.close() | |
| s2.close() | |
| print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment