Create a gist now

Instantly share code, notes, and snippets.

[nsnd struga] Simple hub/worker architecture
import zmq
from time import sleep
from random import random
from json import loads, dumps
HUB_ADDR = 'tcp://127.0.0.1:5555'
DEALER_ADDR = 'tcp://127.0.0.1:5566'
import sys
IDENT = sys.argv[1]
def main():
ctx = zmq.Context()
worker_ = ctx.socket(zmq.REQ)
worker_.connect(DEALER_ADDR)
pusher = ctx.socket(zmq.PUSH)
pusher.connect(HUB_ADDR)
while True:
worker_.send('')
print ' ', IDENT, 'waiting for job...'
num = worker_.recv()
print ' ', IDENT, 'job received:', num
num = int(num)
num +=1
data = dumps((num, IDENT))
print ' ', IDENT, 'working...'
sleep(random()*3)
pusher.send(data)
print ' ', IDENT, 'data sent: ', data
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment