Python - 创建超时(终止子进程)函数的问题
我需要你们的帮助,我在写一个小程序,这个程序会创建一个子进程,执行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
代码:
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
一个子进程可以通过以下方式被终止:
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
0
你的异常叫做 Alarm
,但是你抛出的却是 alarm
。在Python中,大小写是有区别的。
你可能想把 Alarm
改个更好理解的名字。像 AlarmException
或者 AlarmError
这样的名字会让代码更清晰。
2
你可能应该用异常处理,而不是用警报:
class AlarmException(Exception):
pass
...
def alarm_handler(signum, frame):
raise AlarmException()
另外,别忘了管道可能会出现溢出。如果进程产生的数据太多(在某些Linux系统上超过64k),那么程序就会被卡住。