当前位置: 首页>>代码示例>>Python>>正文


Python Process.send_signal方法代码示例

本文整理汇总了Python中psutil.Process.send_signal方法的典型用法代码示例。如果您正苦于以下问题:Python Process.send_signal方法的具体用法?Python Process.send_signal怎么用?Python Process.send_signal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在psutil.Process的用法示例。


在下文中一共展示了Process.send_signal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _kill

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import send_signal [as 别名]
def _kill(pid, timeout=1.5):
    try:
        process = Process(pid)
    except NoSuchProcess:
        return
    try:
        process.send_signal(SIGINT)
        sleep(timeout)
    except OSError:
        pass
    finally:
        try:
            process.send_signal(SIGKILL)
        except (OSError, NoSuchProcess):
            pass
        process.wait()
开发者ID:NAMD,项目名称:pypelinin,代码行数:18,代码来源:test_pipeliner.py

示例2: test_sigint

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import send_signal [as 别名]
    def test_sigint(self):
        '''
        Setup test server sleep 0.01 for each request
        Start spider in separate python shell (untill sigin
            or max 200 requests)
        Wait 1 sec (~100 requests, in reality less
            because of process start-up time)
        Send SIGINT to the process
        Check it returned with 13 or 139 codes
        139 code means segfault (yeah...o_O) But as I see from logs
            it segfaults after successfully processing the SIGINT and
            this is all I need from this test
        '''
        #logging.error('step-0')
        # pylint: disable=no-member
        self.server.response['sleep'] = 0.01
        # pylint: enable=no-member
        with temp_file() as path:
            with open(path, 'w') as out:
                # pylint: disable=no-member
                out.write(self.script_tpl % ('', self.server.get_url()))
                # pylint: enable=no-member
            ret_codes = []
            for _ in range(10):
                #logging.error('step-1')
                proc = Popen('python %s' % path, shell=True)
                #logging.error('step-2')
                parent = Process(proc.pid)
                #logging.error('step-3')
                time.sleep(1)
                #logging.error('killing children')
                for child in parent.children():
                    #logging.error('CHILD: %s', child.pid)
                    # Sending multiple SIGINTs
                    # because in very rare cases the only
                    # sigint signals is ignored :-/
                    # do not send too fast
                    for _ in range(1):
                        try:
                            #logging.error('sending sigint')
                            child.send_signal(SIGNAL_INT)
                        except NoSuchProcess:
                            break
                        else:
                            time.sleep(1)
                if platform.system() == 'Darwin':
                    # On OSX the Popen(shell=True) spawns only
                    # one process, no child
                    #logging.error('Killing parent')
                    #logging.error('PARENT: %s', parent.pid)
                    # Sending multiple SIGINTs
                    # because in very rare cases the only
                    # sigint signals is ignored :-/
                    # do not send too fast
                    for _ in range(1):
                        try:
                            #logging.error('sending sigint')
                            parent.send_signal(SIGNAL_INT)
                        except NoSuchProcess:
                            break
                        else:
                            time.sleep(1)
                #logging.error('step-4')
                ret = None
                for _ in range(20):
                    #print('before proc-poll-%d' % step)
                    ret = proc.poll()
                    if ret is not None:
                        break
                    time.sleep(0.1)
                else:
                    #logging.error('CHILD PROCESS DID NOT RETURN')
                    #raise Exception('Child process did not return')
                    # try to clean processes
                    try:
                        for child in parent.children():
                            child.send_signal(signal.SIGTERM)
                    except NoSuchProcess:
                        pass
                    time.sleep(0.5)
                    try:
                        parent.send_signal(signal.SIGTERM)
                    except NoSuchProcess:
                        pass
                #logging.error('step-5')
                # FIXME: find out the reasonf of segfault
                # the 130 signal means the program was terminated by ctrl-c
                #print('RET CODE: %s' % ret)
                ret_codes.append(ret)

            # Could fail in 10% (1 of 10)
            # pylint: disable=no-member
            self.assertTrue(sum(1 for x in ret_codes
                                if x in (13, 130, 139)) >= 9)
开发者ID:lorien,项目名称:grab,代码行数:96,代码来源:spider_sigint.py


注:本文中的psutil.Process.send_signal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。