Created
February 22, 2014 08:18
-
-
Save minrk/9150423 to your computer and use it in GitHub Desktop.
Examples for SO 21945864
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
import zmq | |
ctx = zmq.Context.instance() | |
s = ctx.socket(zmq.PUSH) | |
url = 'tcp://127.0.0.1:5555' | |
s.connect(url) | |
while True: | |
msg = raw_input("msg > ") | |
s.send(msg) | |
if msg == 'quit': | |
break | |
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
import time | |
import zmq | |
ctx = zmq.Context.instance() | |
s = ctx.socket(zmq.PULL) | |
url = 'tcp://127.0.0.1:5555' | |
s.bind(url) | |
while True: | |
try: | |
msg = s.recv(zmq.NOBLOCK) | |
except zmq.Again: | |
# no message to recv, do other things | |
time.sleep(1) | |
else: | |
print("received %r" % msg) | |
if msg == 'quit': | |
break | |
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
import time | |
import zmq | |
ctx = zmq.Context.instance() | |
s = ctx.socket(zmq.PULL) | |
url = 'tcp://127.0.0.1:5555' | |
s.bind(url) | |
poller = zmq.Poller() | |
poller.register(s) | |
while True: | |
events = dict(poller.poll(0)) | |
if s in events: | |
msg = s.recv() | |
print("received %r" % msg) | |
if msg == 'quit': | |
break | |
else: | |
# no message to recv, do other things | |
time.sleep(1) |
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
import time | |
import zmq | |
ctx = zmq.Context.instance() | |
s = ctx.socket(zmq.PULL) | |
url = 'tcp://127.0.0.1:5555' | |
s.bind(url) | |
from zmq.eventloop import ioloop | |
from zmq.eventloop.zmqstream import ZMQStream | |
def print_msg(msg): | |
print("received %r" % ' '.join(msg)) | |
if msg[0] == 'quit': | |
ioloop.IOLoop.instance().stop() | |
# register the print_msg callback to be fired | |
# whenever there is a message on our socket | |
stream = ZMQStream(s) | |
stream.on_recv(print_msg) | |
# do other things in the meantime | |
tic = time.time() | |
def do_other_things(): | |
print("%.3f" % (time.time() - tic)) | |
pc = ioloop.PeriodicCallback(do_other_things, 1000) | |
pc.start() | |
# start the eventloop | |
ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For SO 21945864