Last active
December 14, 2015 15:59
-
-
Save tchap/5111970 to your computer and use it in GitHub Desktop.
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 | |
# | |
# Requires libevent, greenlet, gevent and pyzmq. | |
# | |
# Exemplar run: | |
# | |
# $ ./poblano_backend.py | |
# GitHub handled by worker 1: OK | |
# Jenkins handled by worker 2: OK | |
# Review Board handled by worker 1: OK | |
# Pivotal Tracker handled by worker 2: OK | |
import gevent | |
import zmq.green as zmq | |
# Settings. | |
POBLANO = 'inproc://poblano' | |
POBLANO_HUB = 'inproc://hub' | |
WORKERS = 2 | |
# 0MQ context used in this example. No I/O threads since we are inproc. | |
ctx = zmq.Context(0) | |
# The Hub greenlet forward messages to workers. | |
def poblano_hub(): | |
frontend = ctx.socket(zmq.ROUTER) | |
frontend.bind(POBLANO) | |
backend = ctx.socket(zmq.DEALER) | |
backend.bind(POBLANO_HUB) | |
zmq.device(zmq.QUEUE, frontend, backend) | |
# The Worker greenlet just appends its ID and returns the message. | |
def poblano_worker(wid): | |
frontend = ctx.socket(zmq.DEALER) | |
frontend.connect(POBLANO_HUB) | |
while 1: | |
msg = frontend.recv_multipart() | |
msg.append('handled by worker {0}: OK'.format(wid)) | |
frontend.send_multipart(msg) | |
# Now let's spawn our greenlets (kinda processes or threads). | |
gevent.spawn(poblano_hub) | |
# Give the hub greenlet some time to bind the socket. Specific to inproc. | |
print('Waiting a second for the hub process to bind properly...') | |
gevent.sleep(1) | |
for i in range(1, WORKERS+1): | |
gevent.spawn(poblano_worker, i) | |
# Initialize the exemplar client. Send some messages and wait for the replies. | |
client = ctx.socket(zmq.DEALER) | |
client.connect(POBLANO) | |
jobs = ['GitHub', 'Jenkins', 'Review Board', 'Pivotal Tracker'] | |
for j in jobs: | |
client.send(j) | |
poller = zmq.Poller() | |
poller.register(client, zmq.POLLIN) | |
for _i in range(len(jobs)): | |
poller.poll() | |
msg = client.recv_multipart() | |
print(' '.join(msg)) | |
# Don't bother with any cleanup, everything gets destroyed at once anyway... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment