def _sendJobCompletionAck(self): for chan in self.inputConnections.values(): try: chan.write(b'a') #whatever chan.flush() except BrokenPipeError: warning("input connection closed")
def handleConfigInput(self, workerConfig): try: cfgObj = json.loads(workerConfig) if (not 'workername' in cfgObj): raise ValueError('The worker name is mandatory') name = cfgObj['workername'] action = False if ("action" in cfgObj.keys()): debug("Got action for worker, skipping config check") action = True else: debug("Checking config...", 1) if (not config_checker.checkWorkerConfigSanity(workerConfig)): return debug("Config is OK", 1) if (name in self.workers): debug("Worker found: " + name, 1) self._sendToWorker(name, workerConfig) else: if (action): raise ValueError( 'The worker must exist to pass an action to it') self.startWorker(workerConfig, name) except: warning("Worker received an invalid configuration : " + str(workerConfig)) time.sleep(1)
def _clientTarget(self, sock): chan = sock.makefile("rwb") while (self.running): try: j = network.readString(chan) out = network.OK try: sa = self._detectSpecialAction(j) if (sa != None): out = sa else: self.handleConfigInput(j) network.sendString(chan, out) except Exception as e: network.sendString(chan, repr(e)) raise e except struct.error: break except: traceback.print_exc() break warning("Closing client connection", 1) sock.shutdown(socket.SHUT_RDWR) sock.close()
def checkAction(self, config): try: if ("action" in config.keys()): debug("Handling action: " + config['action']) action = getattr(self, "action_" + config['action']) action() return True except: warning("Unknown action.") pass return False
def _clientInTarget(self, sock): binChan = sock.makefile("wrb") #w for sending back ack self.inputConnections[sock] = binChan try: while (not self.workerShutdown.value): p = Packet() p.read(binChan) self.inputQueue.put(p) #hold for next packet if Queue is full except: if (_DEBUG_LEVEL == 3): traceback.print_exc() warning("Incoming Connection was lost") finally: self._closeSock(sock) del self.inputConnections[sock] self.action_halt()
def loop(self, data): if data is None: return try: image = data.img except: warning("nothing to do.",2) time.sleep(0.2) return data results = self.detector.run(image) image = self.detector.draw_boxes(results, image) if self.debug_boxing is True: data['img'] = image # COMMENT OUT TO UNDO BOXES DRAWING data['detection'] = encode_results(results) return data
def _test(self, testConnect = True): #prof.enter("SUPERVISOR_TEST") if(not self._matchAdress()): #adress resolving try: res = socket.gethostbyname_ex(self.addr[0]) # (hostname, aliaslist, ipaddrlist) debug("Resolved "+str(self.addr[0])+" to "+str(res[2])) self.addr = (res[2][0], self.addr[1]) except: traceback.print_exc() raise ValueError("For string: "+self.name) if(self.addr[0] == "127.0.0.1"): #FIXME warning("NOTE: Loopback usage is strongly discouraged", 0) if(testConnect and not self._testConnection()): raise ValueError("Unreacheable Supervisor for unit "+self.name)
def _childWorkerAckTarget(self, sock): """ Receives Acknoledgement info: the transmitted Packed has been processed and its ok to send another """ debug("Started ChildWorkerNetworkACK", 3) binChan = sock.makefile("rb") try: while (not self.workerShutdown.value): if (binChan.read(1) != b'a' and not self.workerShutdown.value): #magic value raise ValueError("Wrong ack value") self.outputWorkerLocks[sock].set() debug("Ack from " + str(sock.getpeername()), 3) #Let the unblock if it is a distribute network bahaviour self.globalOutputLock.set() except: traceback.print_exc() warning("Output network callback error", 0) self._closeSock(sock) return
proc.stdin.write(config + "\n") proc.stdin.flush() if __name__ == '__main__': sup = Supervisor() debug("Ready for input", 1) while (True): try: l = input().strip() try: json.loads(l) except Exception as exc: if (_DEBUG_LEVEL >= 3): traceback.print_exc() warning('Invalid Configuration provided') act = sup._detectSpecialAction(l) if (act): print(act) continue sup.handleConfigInput(l) except KeyboardInterrupt: sup.stop() except EOFError: time.sleep(1) except: if (not sup.running):