Python - 创建超时(kill subprocess)函数的问题

时间:2012-01-06 14:25:39

标签: python timeout subprocess kill

我需要在我的小程序中支持你,它创建一个子进程,这里是10次ping,应该被给定的超时值(5)杀死。此示例未成功。 你能给我一个暗示吗?

的问候。 斯蒂芬

输出:

Traceback (most recent call last):
File "./nwcheck.py.work", line 146, in <module>
MyCheck().check().exit()
File "./nwcheck.py.work", line 80, in check
output = process.communicate()
File "/usr/lib/python2.6/subprocess.py", line 701, in communicate
return self._communicate(input)
File "/usr/lib/python2.6/subprocess.py", line 1199, in _communicate
rlist, wlist, xlist = select.select(read_set, write_set, [])
File "./nwcheck.py.work", line 29, in alarm_handler
raise alarm
TypeError: exceptions must be old-style classes or derived from BaseException, not   builtin_function_or_method

CODE:

def check(self):

    class Alarm(Exception):
        pass

    def alarm_handler(signum, frame):
        raise alarm

    def get_process_children(pid):
        p = Popen('ps --no-headers -o pid --ppid %d' % pid, shell = True,
                   stdout = PIPE, stderr = PIPE)
        stdout, stderr = p.communicate()
        return [int(p) for p in stdout.split()]
    timeout = 5
    args2 = [
            'ping',
            'localhost',
            '-c 10',
            ]
    process = subprocess.Popen(args2, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env={'LANG':'de_DE@euro'})
    processpid = process.pid
    print processpid
    if timeout != -1:
        signal(SIGALRM, alarm_handler)
        alarm(timeout)
        print 'in timeout abfrage'
    try:
        ## catch stdout and stderr
        output = process.communicate()
        if timeout != -1:
            alarm(0)
            print 'in timeout abfrage 2'
    except Alarm:
        pids = [process.pid]
        print pids
        if kill_tree:
            pids.extend(get_process_children(process.pid))
        for pid in pids:
        # process might have died before getting to this line
        # so wrap to avoid OSError: no such process
            try:
                kill(pid, SIGKILL)
            except OSError:
                pass
        return -9, '', ''

    # Return a response
    return output

3 个答案:

答案 0 :(得分:2)

您应该使用异常而不是警报:

class AlarmException(Exception): 
    pass 

...

    def alarm_handler(signum, frame):
        raise AlarmException()

另外,不要忘记管道可以overflow。如果该过程对stderr和/或stdout产生过多(在某些Linux上为> 64k),程序将被阻止。

答案 1 :(得分:0)

子进程可以通过以下方式终止:

import os
os.kill(process.pid, signal.SIGKILL)

或者也许:

from subprocess import Popen
Popen.kill()

或:

from subprocess import Popen
Popen.terminate()

请参阅:http://docs.python.org/library/subprocess.html#popen-objects

答案 2 :(得分:0)

您的例外名称为Alarm,但您正在提出alarm。 Python区分大小写。

您可能希望将Alarm重命名为更具描述性的内容。像AlarmExceptionAlarmError这样简单的名称可以使代码更清晰。