def phase(self, value): self._phase = value logger.info( f"{Color.YELLOW.format('PID:')} {self.pid} " f"{Color.YELLOW.format('Start phase:')} " f"{self.phase.decode()[-3::]} " )
def control_process_phase(process: Process, pool: ProcessPool) -> None: def parse_phase_and_update_process() -> None: line = stdout.readline().upper() phase = None if PlottingPhase.SECOND in line: phase = PlottingPhase.SECOND elif PlottingPhase.THIRD in line: phase = PlottingPhase.THIRD elif PlottingPhase.FOUR in line: phase = PlottingPhase.FOUR if not phase: return process.phase = phase logger.info( f'{Color.YELLOW.format("Running pool:")} {pool.running_phases()}' ) stdout = process.stdout while True: code = process.instance.poll() if code is not None: break parse_phase_and_update_process() if code == 0: logger.info( f'{Color.GREEN.format("Finished PID:")} {process.pid} ' f'{Color.GREEN.format("Status code:")} {code} ' f'{Color.GREEN.format("Total time:")} {(time.time() - process.start_time) / 3600} hours') else: logger.error( f'{Color.RED.format("Failed PID: %s")} {process.pid} ' f'{Color.RED.format("Status code:")} {code} ') pool.running.remove(process)
def start_listener(): try: self.loop.run_until_complete( self.listener.listen()) except KeyboardInterrupt: self.loop.close() logger.info( Color.GREEN.format('Down %s') % self.listener.__class__.__name__ )
def handle_user_commands(self): while self.queue.size != 0: command = self.queue.get().get('command') if command == Command.PAUSE: self.pause_spawn = True if command == Command.RESUME: self.pause_spawn = False logger.info( f"{Color.BLUE.format('@@@ Handle user command:')} {command}" )
async def listen(self): server = await asyncio.start_server( self._handle, self.host, self.port ) logger.info( Color.GREEN.format('%s listen on %s:%s') % ( self.__class__.__name__, self.host, self.port ) ) await server.serve_forever()
def parse_phase_and_update_process() -> None: line = stdout.readline().upper() phase = None if PlottingPhase.SECOND in line: phase = PlottingPhase.SECOND elif PlottingPhase.THIRD in line: phase = PlottingPhase.THIRD elif PlottingPhase.FOUR in line: phase = PlottingPhase.FOUR if not phase: return process.phase = phase logger.info( f'{Color.YELLOW.format("Running pool:")} {pool.running_phases()}' )
async def _handle(self, reader, writer): while True: command = await self._receive(reader) if not command: continue if command not in Command.values(): writer.write(b'Unknown command') continue self.queue.put({ 'date': datetime.datetime.utcnow().isoformat(), 'command': command }) writer.write(b'Command put in queue') logger.info( f'{Color.BLUE.format("@@@ Received command:")} %s' % command )
def spawn(self): awaiting = self.pool.awaiting.pop() awaiting.run() running, awaiting = awaiting, Process(settings.PLOTTER_RUN_CMD) thread = threading.Thread( target=control_process_phase, args=(running, self.pool) ) thread.start() self.pool.add(running) self.pool.add(awaiting) logger.info( f'{Color.GREEN.format("Run PID:")} {running.pid} ' f'{Color.GREEN.format("Running total:")} {self.pool.running_total()} ' f'{Color.GREEN.format("Awaiting total:")} {self.pool.awaiting_total()} ' f'{Color.GREEN.format("Running pool:")} {self.pool.running_phases()}' )
async def commands_client(loop, config): try: reader, writer = await asyncio.open_connection(*config.LISTENER_ADDR, loop=loop) except ConnectionRefusedError as e: logger.error(Color.RED.format(e)) return logger.info( Color.GREEN.format('Connected on %s:%s') % config.LISTENER_ADDR) time.sleep(1) while True: try: message = input(Color.YELLOW.format('Input [pause|resume]: ')) writer.write(message.encode()) data = await reader.read(1024) logger.info(Color.GREEN.format('Response: %s') % data.decode()) time.sleep(0.5) except KeyboardInterrupt: command = input( Color.BLUE.format('Close client? [yes/no]: ')).lower() if command == 'yes': break writer.close() logger.info(Color.GREEN.format('Commands client: Close'))
def killall(self) -> None: for process in self.pool.running: process.kill() logger.info(f'{Color.RED.format("Kill process:")} {process.pid}')