进程管理类
| 16 | |
| 17 | |
| 18 | class ProcMgr(object): |
| 19 | """ |
| 20 | 进程管理类 |
| 21 | """ |
| 22 | def kill_proc_famliy(self, pid): |
| 23 | """ |
| 24 | 杀掉进程,并递归杀掉其子孙进程 |
| 25 | :param pid: |
| 26 | :return: |
| 27 | """ |
| 28 | try: |
| 29 | task_proc = psutil.Process(pid) |
| 30 | children = task_proc.children(recursive=True) |
| 31 | logger.info("kill process: %s" % task_proc) |
| 32 | task_proc.terminate() # 关闭进程 |
| 33 | # 关闭子孙进程 |
| 34 | logger.info("kill children processes: %s" % children) |
| 35 | for child in children: |
| 36 | try: |
| 37 | child.kill() # 发送 kill 信号 |
| 38 | except Exception as err: |
| 39 | logger.error("kill child proc failed: %s" % err) |
| 40 | gone, still_alive = psutil.wait_procs(children, timeout=5) |
| 41 | for child in still_alive: |
| 42 | try: |
| 43 | child.kill() # 如果没有关闭,则再发送 SIGkill 信号 |
| 44 | except Exception as err: |
| 45 | logger.error("kill child proc failed: %s" % err) |
| 46 | except psutil.NoSuchProcess as err: |
| 47 | logger.warning("process is already terminated: %s" % err) |
| 48 | except Exception as err: |
| 49 | logger.error("kill task failed: %s" % err) |
no outgoing calls
no test coverage detected