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
I need support of you in my little program, that creates a subprocess, here a 10 time ping, and should be killed by a given timeout value (5). This example does not succeed.
Can you give me a hint?
Regards.
Stefan
Output:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能应该使用异常而不是警报:
另外,不要忘记管道可以 溢出。如果进程会为 stderr 和/或 stdout 生成太多(在某些 Linux 上大于 64k),则程序将被阻止。
You should probably use exception instead of alarm:
Also, do not forget that pipes can overflow. If the process will produce too much (> 64k on some Linuxes) for stderr and/or stdout, program will be blocked.
子进程可以通过以下方式终止:
或可能:
或:
请参阅: http:// docs.python.org/library/subprocess.html#popen-objects
a subprocess can be killed by:
or maybe:
or:
See this: http://docs.python.org/library/subprocess.html#popen-objects
您的异常名为
Alarm
,但您正在引发alarm
。 Python 区分大小写。您可能需要将
Alarm
重命名为更具描述性的名称。像AlarmException
或AlarmError
这样简单的名称将使代码更清晰。Your exception is named
Alarm
, but you're raisingalarm
. Python is case-sensitive.You may want to rename
Alarm
to something more descriptive. A name as simple asAlarmException
orAlarmError
would make the code clearer.