please, help with python 3.1

Alan Wilter Sousa da Silva awilter at ebi.ac.uk
Tue Aug 3 11:28:49 CEST 2010


Ok:

Here goes the code again and results:

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)


----------------------------------------------------
time python2.6 timout.py
PID: 27644
Timed out! Process 27644 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/python2.6/subprocess.py", line 693, 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!
python2.6 timout.py  0.10s user 0.47s system 11% cpu 5.113 total

#### WORKED as expected ####
----------------------------------------------------

Now with python3.1:

----------------------------------------------------
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.
----------------------------------------------------

I hope it's better now.

Thanks,

Alan

On Tue, Aug 3, 2010 at 08:42, rantingrick <rantingrick at gmail.com> wrote:

> On Aug 3, 2:28 am, Chris Rebert <c... at rebertia.com> wrote:
> > ---------- Forwarded message ----------
> > From: Alan <alanwil... at gmail.com>
> > Date: Tue, Aug 3, 2010 at 12:25 AM
> > Subject: Re: please, help with python 3.1
> > To: Chris Rebert <c... at rebertia.com>
> >
> > Sorry,
> > I will explain.
>
> Well it looks like he forgot to post the traceback!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate
Department of Biochemistry, University of Cambridge.
80 Tennis Court Road, Cambridge CB2 1GA, UK.
>>http://www.bio.cam.ac.uk/~awd28<<
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100803/49f9cd36/attachment.html>


More information about the Python-list mailing list