def start(self): print "Starting Lightsocket..." context = ZMQ.context(4) # number of IO threads to use self.socket = context.socket(ZMQ.REP) self.socket.bind(self.address) self.up = True self.receive_requests()
def run(self): gson = Gson() # Initialize a zeromq context context = ZMQ.context(1) #print('{}> Trying to connect...'.format(wrk_num)) # Set up a channel to receive work from the ventilator work_receiver = context.socket(ZMQ.PULL) work_receiver.connect("tcp://127.0.0.1:5557") # Set up a channel to send result of work to the results reporter results_sender = context.socket(ZMQ.PUSH) results_sender.connect("tcp://127.0.0.1:5558") # Set up a channel to receive control messages over control_receiver = context.socket(ZMQ.SUB) control_receiver.connect("tcp://127.0.0.1:5559") control_receiver.subscribe("") # Set up a poller to multiplex the work receiver and control receiver channels poller = context.poller(2) poller.register(work_receiver, ZMQ.Poller.POLLIN) poller.register(control_receiver, ZMQ.Poller.POLLIN) # Loop and accept messages from both channels, acting accordingly while True: poller.poll() # If the message came from work_receiver channel, square the number # and send the answer to the results reporter #if socks.get(work_receiver) == zmq.POLLIN: if poller.pollin(0): msg = work_receiver.recv(0) msg = msg.tostring() work_message = gson.fromJson(msg, HashMap) num = work_message.get('num') product = num * num answer_message = HashMap() answer_message.put('worker', self.wrk_num) answer_message.put('result', product) answer_message.put('input', num) msg = gson.toJson(answer_message) results_sender.send(msg, 0) # If the message came over the control channel, shut down the worker. #if socks.get(control_receiver) == zmq.POLLIN: if poller.pollin(1): msg = control_receiver.recv(0) control_message = msg.tostring() if control_message == "FINISHED": #print("{}> Received FINSHED, quitting!".format(wrk_num)) break
def run(self): gson = Gson() # Initialize a zeromq context context = ZMQ.context(1) # Set up a channel to receive results results_receiver = context.socket(ZMQ.PULL) results_receiver.bind("tcp://127.0.0.1:5558") # Set up a channel to send control commands control_sender = context.socket(ZMQ.PUB) control_sender.bind("tcp://127.0.0.1:5559") for task_nbr in xrange(NUM): msg = results_receiver.recv(0) msg = msg.tostring() result_message = gson.fromJson(msg, HashMap) #print "Worker %i answered: %i" % (result_message['worker'], result_message['result']) assert result_message['result'] == result_message['input'] * result_message['input'] # Signal to all workers that we are finsihed control_sender.send("FINISHED", 0) time.sleep(1)