Created
April 28, 2014 12:04
-
-
Save markkimsal/11369832 to your computer and use it in GitHub Desktop.
Python PYZMQ PUSH/PULL buffer
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
#!/usr/bin/python | |
import zmq | |
import time | |
import collections | |
ctx = zmq.Context() | |
pull_socket = ctx.socket(zmq.PULL) | |
pull_socket.bind('tcp://127.0.0.1:5590') | |
pull_socket.setsockopt(zmq.LINGER, 0) | |
poller = zmq.Poller() | |
poller.register(pull_socket, zmq.POLLIN) | |
push_socket= ctx.socket(zmq.PUSH) | |
push_socket.bind('tcp://127.0.0.1:5591') | |
push_socket.setsockopt(zmq.LINGER, 0) | |
buf = collections.deque([], 30000) | |
while True: | |
socks = dict(poller.poll(1000)) | |
if socks: | |
if socks.get(pull_socket) == zmq.POLLIN: | |
msg = pull_socket.recv() | |
print msg | |
buf.append(msg) | |
else: | |
pass | |
out = None | |
try: | |
out = buf.popleft() | |
push_socket.send(out, zmq.NOBLOCK) | |
out = None | |
except IndexError: | |
pass | |
except Exception as inst: | |
if out: | |
buf.append(out) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment