Message112771
I have this example code to illustrate a problem I am having with python3. It works fine with python 2.6 and 2.7 but does not with python 3.1.
from __future__ import print_function
import os, subprocess, signal
def signal_handler( signum, frame ):
print( "PID: %s" % pid )
print( "Timed out! Process %s killed, max exec time (%ss) exceeded" % (pid, timeTol ) )
os.kill( int( pid ), 15 )
raise Exception( "Taking too long to finish... aborting!" )
if __name__ == '__main__':
timeTol = 5
cmd = 'find /'
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(timeTol)
p = subprocess.Popen(cmd, shell=True, stderr = subprocess.STDOUT, stdout = subprocess.PIPE)
pid = p.pid
out = str( p.communicate()[0].decode() )
print(out)
Executing it:
time python3.1 timout.py
PID: 27687
Timed out! Process 27687 killed, max exec time (5s) exceeded
Traceback (most recent call last):
File "timout.py", line 23, in <module>
out = str( p.communicate()[0].decode() )
File "/sw/lib/python3.1/subprocess.py", line 719, in communicate
stdout = self.stdout.read()
File "timout.py", line 9, in signal_handler
raise Exception( "Taking too long to finish... aborting!" )
Exception: Taking too long to finish... aborting!
python3.1 timout.py 0.52s user 3.88s system 19% cpu 22.841 total
#### It prints essentially the same thing with a *very* *big* difference it takes 22 seconds and actually the alarm only works when the whole task ('find /') is finished. |
|
Date |
User |
Action |
Args |
2010-08-04 08:33:57 | alanwilter | set | recipients:
+ alanwilter |
2010-08-04 08:33:56 | alanwilter | set | messageid: <1280910836.88.0.645209486589.issue9504@psf.upfronthosting.co.za> |
2010-08-04 08:33:55 | alanwilter | link | issue9504 messages |
2010-08-04 08:33:53 | alanwilter | create | |
|