Reputation: 271
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
Upvotes: 0
Views: 625
Reputation: 4199
You should probably use exception instead of alarm:
class AlarmException(Exception):
pass
...
def alarm_handler(signum, frame):
raise AlarmException()
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.
Upvotes: 2
Reputation: 2030
Your exception is named Alarm
, but you're raising alarm
. Python is case-sensitive.
You may want to rename Alarm
to something more descriptive. A name as simple as AlarmException
or AlarmError
would make the code clearer.
Upvotes: 0
Reputation: 72
a subprocess can be killed by:
import os
os.kill(process.pid, signal.SIGKILL)
or maybe:
from subprocess import Popen
Popen.kill()
or:
from subprocess import Popen
Popen.terminate()
See this: http://docs.python.org/library/subprocess.html#popen-objects
Upvotes: 0