def start_manager(length, sub_len): context = zmq.Context() print("Doing a distributed sum of an array %d long" % length) to_sum = range(length) task_send_sock = context.socket(zmq.PUSH) task_send_sock.bind(TASK_ADDR) sent, idx = 0, 0 num_pkts = int(ceil(float(sent) / sub_len)) while sent < num_pkts: start, end = idx, idx+sub_len print("Sending sub-array from %d to %d" % (start, end)) task_msg = Task(to_sum[start:end]) task_send_sock.send(task_msg.dump()) sent += 1 idx += sub_len print("Sent all the messages")
def start_worker(idx): print("Starting worker %d" % idx) context = zmq.Context() res_send_sock = context.socket(zmq.PUB) res_send_sock.connect(RESULT_ADDR) task_recv_sock = context.socket(zmq.PULL) task_recv_sock.connect(TASK_ADDR) while True: task_msg = task_recv_sock.recv() subarray = Task.load(task_msg).subarray print("Worker %d doing amazing computation on an array %d long" % (idx, len(subarray))) res = Result(idx, sum(subarray)) res_send_sock.send(res.dump()) # TODO: check why we always get the same worker do the job sleep(randrange(5))