| 8 | |
| 9 | |
| 10 | class ScriptProcess(Process): |
| 11 | |
| 12 | def __init__(self, config: str, port: int, log_queue: Queue, update_queue: Queue) -> None: |
| 13 | """ |
| 14 | |
| 15 | :param port: 端口 tcp 127.0.0.1:port |
| 16 | :param config: 如oas1 |
| 17 | :param log_queue: 一个输出到gui的Log队列 |
| 18 | """ |
| 19 | super().__init__() |
| 20 | self.config = config |
| 21 | self.port = port |
| 22 | self.name = config |
| 23 | self.log_queue = log_queue |
| 24 | self.update_queue = update_queue |
| 25 | self.daemon = True # 设置为守护进程,主进程结束,子进程也结束 |
| 26 | |
| 27 | |
| 28 | |
| 29 | @property |
| 30 | def alive(self) -> bool: |
| 31 | """ |
| 32 | alive |
| 33 | :return: |
| 34 | """ |
| 35 | return self.is_alive() |
| 36 | |
| 37 | def run(self) -> None: |
| 38 | """ |
| 39 | run |
| 40 | :return: |
| 41 | """ |
| 42 | self.start_log() |
| 43 | try: |
| 44 | from script import Script |
| 45 | script = Script(config_name=self.config) |
| 46 | script.gui_update_task = self.update_tasks |
| 47 | script.init_server(self.port) |
| 48 | script.run_server() |
| 49 | except: |
| 50 | logger.exception(f'run script {self.config} error') |
| 51 | raise |
| 52 | |
| 53 | def stop(self) -> None: |
| 54 | """ |
| 55 | stop the process |
| 56 | 是强制的,不会等待子进程结束 |
| 57 | :return: |
| 58 | """ |
| 59 | self.terminate() |
| 60 | self.join() |
| 61 | logger.info(f'stop script {self.config}') |
| 62 | |
| 63 | # def restart(self) -> None: |
| 64 | # """ |
| 65 | # restart the process |
| 66 | # :return: |
| 67 | # """ |