Ok:<div><br></div><div>Here goes the code again and results:</div><div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>from __future__ import print_function</div>
<div>import os, subprocess, signal</div><div><br></div><div>def signal_handler( signum, frame ):</div><div><br></div><div> print( "PID: %s" % pid )</div><div> print( "Timed out! Process %s killed, max exec time (%ss) exceeded" % (pid, timeTol ) )</div>
<div> os.kill( int( pid ), 15 )</div><div> raise Exception( "Taking too long to finish... aborting!" )</div><div><br></div><div>if __name__ == '__main__':</div><div><br></div><div> timeTol = 5</div>
<div><br></div><div> cmd = 'find /'</div><div><br></div><div> signal.signal(signal.SIGALRM, signal_handler)</div><div> signal.alarm(timeTol)</div><div><br></div><div> p = subprocess.Popen(cmd, shell=True, stderr = subprocess.STDOUT, stdout = subprocess.PIPE)</div>
<div> pid = p.pid</div><div><br></div><div> out = str( p.communicate()[0].decode() )</div><div> print(out)</div><div><br></div></span><div><br></div><div>----------------------------------------------------</div>
<div><div><div><div><div>time python2.6 timout.py</div><div>PID: 27644</div><div>Timed out! Process 27644 killed, max exec time (5s) exceeded</div><div>Traceback (most recent call last):</div><div> File "timout.py", line 23, in <module></div>
<div> out = str( p.communicate()[0].decode() )</div><div> File "/sw/lib/python2.6/subprocess.py", line 693, in communicate</div><div> stdout = self.stdout.read()</div><div> File "timout.py", line 9, in signal_handler</div>
<div> raise Exception( "Taking too long to finish... aborting!" )</div><div>Exception: Taking too long to finish... aborting!</div><div>python2.6 timout.py 0.10s user 0.47s system 11% cpu 5.113 total</div></div>
</div></div><div><br></div><div>#### WORKED as expected ####</div><div>----------------------------------------------------</div><div><br></div><div>Now with python3.1:</div><div><br></div><div>----------------------------------------------------</div>
<div><div>time python3.1 timout.py</div><div>PID: 27687</div><div>Timed out! Process 27687 killed, max exec time (5s) exceeded</div><div>Traceback (most recent call last):</div><div> File "timout.py", line 23, in <module></div>
<div> out = str( p.communicate()[0].decode() )</div><div> File "/sw/lib/python3.1/subprocess.py", line 719, in communicate</div><div> stdout = self.stdout.read()</div><div> File "timout.py", line 9, in signal_handler</div>
<div> raise Exception( "Taking too long to finish... aborting!" )</div><div>Exception: Taking too long to finish... aborting!</div><div>python3.1 timout.py 0.52s user 3.88s system 19% cpu 22.841 total</div>
</div>
<div><br></div><div>#### 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.</div><div>----------------------------------------------------</div>
<div><br></div><div>I hope it's better now.</div><div><br></div><div>Thanks,</div><div><br></div><div>Alan</div><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 08:42, rantingrick <span dir="ltr"><<a href="mailto:rantingrick@gmail.com">rantingrick@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">On Aug 3, 2:28 am, Chris Rebert <<a href="mailto:c...@rebertia.com">c...@rebertia.com</a>> wrote:<br>
<div class="im">> ---------- Forwarded message ----------<br>
> From: Alan <<a href="mailto:alanwil...@gmail.com">alanwil...@gmail.com</a>><br>
> Date: Tue, Aug 3, 2010 at 12:25 AM<br>
> Subject: Re: please, help with python 3.1<br>
</div>> To: Chris Rebert <<a href="mailto:c...@rebertia.com">c...@rebertia.com</a>><br>
><br>
> Sorry,<br>
> I will explain.<br>
<br>
Well it looks like he forgot to post the traceback!<br>
<font color="#888888">--<br>
</font><div><div></div><div class="h5"><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate<br>Department of Biochemistry, University of Cambridge. <br>80 Tennis Court Road, Cambridge CB2 1GA, UK.<br>
>><a href="http://www.bio.cam.ac.uk/~awd28">http://www.bio.cam.ac.uk/~awd28</a><<<br>
</div></div>