def communicate(self): conn = yield bluelet.connect(self.host, self.port) yield cw._sendmsg(conn, cw.WorkerRegisterMessage()) connected = True try: while True: # Get a task from the master. msg = yield cw._readmsg(conn) if msg is None: print('connection to master closed') connected = False break assert isinstance(msg, cw.TaskMessage) try: with chdir(msg.cwd): func = cw.func_deser(msg.func_blob) args = cw.slow_deser(msg.args_blob) kwargs = cw.slow_deser(msg.kwargs_blob) res = func(*args, **kwargs) except: res = format_remote_exc() response = cw.ResultMessage(msg.jobid, False, cw.slow_ser(res)) else: response = cw.ResultMessage(msg.jobid, True, cw.slow_ser(res)) yield cw._sendmsg(conn, response) finally: if connected: yield cw._sendmsg(conn, cw.WorkerDepartMessage())
def communicate(self): conn = yield bluelet.connect(self.host, self.port) yield cw._sendmsg(conn, cw.WorkerRegisterMessage()) connected = True try: while True: # Get a task from the master. msg = yield cw._readmsg(conn) if msg is None: print('connection to master closed') connected = False break assert isinstance(msg, cw.TaskMessage) try: with chdir(msg.cwd): func = cw.func_deser(msg.func_blob) args = cw.slow_deser(msg.args_blob) kwargs = cw.slow_deser(msg.kwargs_blob) res = func(*args, **kwargs) except Exception as exception: # Check if the function tells us to shut down this worker. # We can't just catch the exception normally - # for some reason between serialization things get # fouled up. if type(exception).__name__ == 'ShutdownSignal': res = 'shutdown signal received' response = cw.ResultMessage( msg.jobid, True, cw.slow_ser(res), True) yield cw._sendmsg(conn, response) break res = format_remote_exc() response = cw.ResultMessage(msg.jobid, False, cw.slow_ser(res), False) else: response = cw.ResultMessage(msg.jobid, True, cw.slow_ser(res), False) yield cw._sendmsg(conn, response) finally: if connected: yield cw._sendmsg(conn, cw.WorkerDepartMessage())
def handle_results(self, callback): self.conn = yield bluelet.connect(self.host, self.port) self.connection_ready() while True: result = yield cw._readmsg(self.conn) if result is None: print('server connection closed') return assert isinstance(result, cw.ResultMessage) callback(result.jobid, result.success, cw.slow_deser(result.result_blob))