博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python kil 掉子进程
阅读量:5814 次
发布时间:2019-06-18

本文共 3132 字,大约阅读时间需要 10 分钟。

  hot3.png

def _stop_unix(self, errors):        """        UNIX implementation of process killing        @param errors: error messages. stop() will record messages into this list.        @type  errors: [str]        """        self.exit_code = self.popen.poll()         if self.exit_code is not None:            logger.debug("process[%s].stop(): process has already returned %s", self.name, self.exit_code)            #print "process[%s].stop(): process has already returned %s"%(self.name, self.exit_code)            self.popen = None            self.stopped = True            return        pid = self.popen.pid        pgid = os.getpgid(pid)        logger.info("process[%s]: killing os process with pid[%s] pgid[%s]", self.name, pid, pgid)        try:            # Start with SIGINT and escalate from there.            logger.info("[%s] sending SIGINT to pgid [%s]", self.name, pgid)            os.killpg(pgid, signal.SIGINT)            logger.info("[%s] sent SIGINT to pgid [%s]", self.name, pgid)            timeout_t = time.time() + _TIMEOUT_SIGINT            retcode = self.popen.poll()            while time.time() < timeout_t and retcode is None:                time.sleep(0.1)                retcode = self.popen.poll()            # Escalate non-responsive process            if retcode is None:                printerrlog("[%s] escalating to SIGTERM"%self.name)                timeout_t = time.time() + _TIMEOUT_SIGTERM                os.killpg(pgid, signal.SIGTERM)                logger.info("[%s] sent SIGTERM to pgid [%s]"%(self.name, pgid))                retcode = self.popen.poll()                while time.time() < timeout_t and retcode is None:                    time.sleep(0.2)                    logger.debug('poll for retcode')                    retcode = self.popen.poll()                if retcode is None:                    printerrlog("[%s] escalating to SIGKILL"%self.name)                    errors.append("process[%s, pid %s]: required SIGKILL. May still be running."%(self.name, pid))                    try:                        os.killpg(pgid, signal.SIGKILL)                        logger.info("[%s] sent SIGKILL to pgid [%s]"%(self.name, pgid))                        # #2096: don't block on SIGKILL, because this results in more orphaned processes overall                        #self.popen.wait()                        #os.wait()                        logger.info("process[%s]: sent SIGKILL", self.name)                    except OSError as e:                        if e.args[0] == 3:                            printerrlog("no [%s] process with pid [%s]"%(self.name, pid))                        else:                            printerrlog("errors shutting down [%s], see log for details"%self.name)                            logger.error(traceback.format_exc())                else:                    logger.info("process[%s]: SIGTERM killed with return value %s", self.name, retcode)            else:                logger.info("process[%s]: SIGINT killed with return value %s", self.name, retcode)        finally:            self.popen = None

参考:

转载于:https://my.oschina.net/itfanr/blog/2050677

你可能感兴趣的文章
定时任务的创建
查看>>
实战Django:小型CMS Part2
查看>>
原创]windows server 2012 AD架构试验系列 – 16更改DC计算机名
查看>>
统治世界的十大算法
查看>>
linux svn安装和配置
查看>>
SSH中调用另一action的方法(chain,redirect)
查看>>
数据库基础
查看>>
表格排序
查看>>
关于Android四大组件的学习总结
查看>>
java只能的round,ceil,floor方法的使用
查看>>
由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件...
查看>>
新开的博客,为自己祝贺一下
查看>>
puppet任务计划
查看>>
【CQOI2011】放棋子
查看>>
采用JXL包进行EXCEL数据写入操作
查看>>
一周总结
查看>>
将txt文件转化为json进行操作
查看>>
线性表4 - 数据结构和算法09
查看>>
C语言数据类型char
查看>>
Online Patching--EBS R12.2最大的改进
查看>>