kill the task
(self)
| 78 | return message |
| 79 | |
| 80 | def terminate(self): |
| 81 | """kill the task""" |
| 82 | if settings.PLATFORMS[sys.platform] == "windows": |
| 83 | # 在windows平台上只调用terminate,只能关闭子进程,无法关闭孙子进程,需要通过递归遍历子进程来关闭 |
| 84 | # 先关闭子进程,原因:子进程如果一直在启动孙进程,先关闭孙进程再关闭子进程,两步之间可能会有孙进程漏关 |
| 85 | try: |
| 86 | task_proc = psutil.Process(self._task_proc.pid) |
| 87 | children = task_proc.children(recursive=True) |
| 88 | self._task_proc.terminate() # 关闭子进程 |
| 89 | logger.info("kill children processes: %s" % children) |
| 90 | for child in children: |
| 91 | try: |
| 92 | child.kill() # 发送 kill 信号 |
| 93 | except Exception as err: |
| 94 | logger.error("kill child proc failed: %s" % err) |
| 95 | gone, still_alive = psutil.wait_procs(children, timeout=5) |
| 96 | for child in still_alive: |
| 97 | try: |
| 98 | child.kill() # 如果没有关闭,则再发送 SIGkill 信号 |
| 99 | except Exception as err: |
| 100 | logger.error("kill child proc failed: %s" % err) |
| 101 | except Exception as err: |
| 102 | logger.error("kill task failed: %s" % err) |
| 103 | else: |
| 104 | # linux/mac平台,调用subprocc模块stop方法中止进程,会使用进程组的方式杀掉子孙进程(windows不支持该方式) |
| 105 | self._task_proc.stop() |
| 106 | # 重置 |
| 107 | self._task_proc = None |
| 108 | |
| 109 | def _check_task_expired(self): |
| 110 | """检查任务是否超时,如果超时,终止任务 |
no test coverage detected