def compute(self): while not self.stop[0]: self.sock = self.connect() if not self.verify(): self.sock.shutdown(2) self.sock.close() time.sleep(DELAY) continue log.info(str(self.key) + ' compute verified.') # 开始任务传输通信 if send(self.sock, 'Request Compute'): msg = receive(self.sock) if msg == 'positive': time.sleep(0.1) if send(self.sock, 'ack'): try: self.sock.shutdown(2) self.sock.close() except Exception: break log.info(self.key + ' compute start.') return True log.info(self.key + ' compute rejected.') try: self.sock.shutdown(2) self.sock.close() except Exception: pass time.sleep(DELAY) self.sock.close() exit()
def verify(self): msg = receive(self.sock) if msg == 'verify': if not self.key: if send(self.sock, 'blank'): return self.register() else: if send(self.sock, self.key): if receive(self.sock) == 'pass': return True return False
def verify(self): if not send(self.request, "verify"): return None msg = receive(self.request) # 若首次连接,客户端需要发送'blank'作为验证信息 if not msg: return None if Handler.slaveRec.__contains__(msg): self.key = msg if send(self.request, "pass"): return 'member' elif self.register(msg): # 节点发送的是未知的key(可能之前的循环中持有并使用过)或为新 # 需要将之前的key值复用,若为blank则注册新的key return 'member' return None
def compute(self): if receive(self.request) == 'Request Compute': # status = np.array(list(Handler.slaveRec.values())) # status = np.logical_or( # np.logical_or(status == 'push', status == 'compute'), # np.logical_or(status == 'pull', status == 'reject')) # if not status.all(): # send(self.request, 'negative') # elif send(self.request, 'positive'): # Handler.slaveRec[self.key] = 'compute' # return True time.sleep(0.1) status = np.array(list(Handler.slaveRec.values())) status = np.logical_or( np.logical_or(status == 'push', status == 'compute'), np.logical_or(status == 'pull', status == 'reject')) while not status.all(): status = np.array(list(Handler.slaveRec.values())) status = np.logical_or( np.logical_or(status == 'push', status == 'compute'), np.logical_or(status == 'pull', status == 'reject')) if send(self.request, 'positive'): if receive(self.request) == 'ack': Handler.slaveRec[self.key] = 'compute' log.debug('computing: %s' % str(self.key)) return True return False
def register(self): msg = receive(self.sock) # type:str if msg.startswith('key:'): if send(self.sock, 'accept'): self.key = msg[4:] return True return False
def register(self, key): if key == 'blank': key = UUID(Handler.slaveRec) if not send(self.request, 'key:' + key): return False msg = receive(self.request) if msg == 'accept': self.key = key Handler.slaveRec[self.key] = 'verify' log.debug('%s are verified.' % str(self.key)) return True else: if send(self.request, 'pass'): self.key = key Handler.slaveRec[self.key] = 'verify' log.debug('%s are verified.' % str(self.key)) return True return False
def bind(self): log.debug('new connection coming: %s' % str(self.key)) Handler.semInput.acquire() if len(Handler.inputList): Handler.taskBinds[self.key] = Handler.inputList.pop() Handler.seqBinds[self.key] = len(Handler.inputList) Handler.slaveRec[self.key] = 'bind' log.debug('<tasks remain = %s>' % len(Handler.inputList)) Handler.semInput.release() log.debug('new connection are bound: %s' % str(self.key)) return True else: log.debug('<tasks remain = %s> : %s' % (len(Handler.inputList), str(self.key))) Handler.slaveRec[self.key] = 'reject' Handler.semInput.release() log.debug('new connection are rejected: %s' % str(self.key)) send(self.request, 'reject') return False
def push(self): # while len(Handler.inputList): # 等待:当所有计算任务分配完毕,则所有线程开始运行 time.sleep(DELAY) if not send(self.request, 'push'): return False ds = DataServer() if not ds.pushData(self.request, Handler.taskBinds[self.key]): return False # if not send(self.request, Handler.taskBinds[self.key]): # return False Handler.slaveRec[self.key] = 'push' log.debug('data have sent to: %s' % str(self.key)) # # 补丁,保证工作节点开始运行时刻相同 # flag = False # if receive(self.request): # flag = True # status = np.array(list(Handler.slaveRec.values())) # while not (status == 'push').all(): # pass # send(self.request, 'accept') # # 补丁结束 return True
def reject(self): if Handler.slaveRec[self.key] != 'reject': Handler.slaveRec[self.key] = 'reject' time.sleep(DELAY) send(self.request, 'reject')