Created
June 10, 2012 14:41
-
-
Save petrushev/2905984 to your computer and use it in GitHub Desktop.
[nsnd struga] Simple hub/worker architecture
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 | |
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