Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Run process with timeout

Reply
Thread Tools

Run process with timeout

 
 
Natan
Guest
Posts: n/a
 
      10-17-2005
Hi.

I have a python script under linux where I poll many hundreds of
interfaces with mrtg every 5 minutes. Today I create some threads and
use os.system(command) to run the process, but some of them just hang.
I would like to terminate the process after 15 seconds if it doesn't
finish, but os.system() doesn't have any timeout parameter.

Can anyone help me on what can I use to do this?

Thank you

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-17-2005
Natan <(E-Mail Removed)> wrote:

> Hi.
>
> I have a python script under linux where I poll many hundreds of
> interfaces with mrtg every 5 minutes. Today I create some threads and
> use os.system(command) to run the process, but some of them just hang.
> I would like to terminate the process after 15 seconds if it doesn't
> finish, but os.system() doesn't have any timeout parameter.
>
> Can anyone help me on what can I use to do this?


Use the subprocess module. With a subprocess.Popen object, you can then
sleep a while, check (with .poll()) if it's finished, otherwise kill it
(use its .pid attribute).


Alex
 
Reply With Quote
 
 
 
 
P@draigBrady.com
Guest
Posts: n/a
 
      10-17-2005
Natan wrote:
> Hi.
>
> I have a python script under linux where I poll many hundreds of
> interfaces with mrtg every 5 minutes. Today I create some threads and
> use os.system(command) to run the process, but some of them just hang.
> I would like to terminate the process after 15 seconds if it doesn't
> finish, but os.system() doesn't have any timeout parameter.
>
> Can anyone help me on what can I use to do this?


The new subprocess module has that functionality.
If your python version doesn't have that you
could try my unix specific version:
http://www.pixelbeat.org/libs/subProcess.py

Pádraig.
 
Reply With Quote
 
Micah Elliott
Guest
Posts: n/a
 
      10-17-2005
On Oct 17, Alex Martelli wrote:
> Natan <(E-Mail Removed)> wrote:
> > I have a python script under linux where I poll many hundreds of
> > interfaces with mrtg every 5 minutes. Today I create some threads and
> > use os.system(command) to run the process, but some of them just hang.
> > I would like to terminate the process after 15 seconds if it doesn't
> > finish, but os.system() doesn't have any timeout parameter.

>
> Use the subprocess module. With a subprocess.Popen object, you can
> ... kill it (use its .pid attribute).


The problem I've run into with this approach is the inability to kill
the pid's children. Most often I'm not so fortunate to be able to
depend on the process to not be doing its own forking. So here's a
simplified use case:

$ cat sleep10.sh
#! /bin/bash
sleep 10 # does not get killed
$
$ cat to3.py
#! /usr/bin/env python

from subprocess import Popen
from time import sleep
from os import kill
from signal import SIGTERM

p = Popen(['./sleep10.sh'])
sleep(3)
kill(p.pid, SIGTERM) # Oops, won't kill p.pid's children.
##kill(-p.pid, SIGTERM) # Won't work since not group leader,
# and I'd rather avoid fork/dup/exec.
$
$ ./to3.py
$ # to3.py finished but sleep 10 still running

If you try this you will see that sleep10.sh gets killed, but its
"sleep 10" subprocess does not, and runs for an additional 7 seconds.

I presently rely on an ugly script to do this properly. It uses low
level calls such as pipe, close, dup2, fork, exec, setpgrp, etc. I
won't post that here for brevity's sake (unless requested). For this
case it would fork/exec sleep10.sh, make it a group leader, and the
parent would kill its group.

Is there any way to enable Python's subprocess module to do (implicit?)
group setup to ease killing of all children? If not, is it a reasonable
RFE?

--
Micah Elliott
<(E-Mail Removed)>
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-17-2005
Micah Elliott <(E-Mail Removed)> wrote:

> Is there any way to enable Python's subprocess module to do (implicit?)
> group setup to ease killing of all children? If not, is it a reasonable
> RFE?


Not as far as I know. It might be a reasonable request in suitable
dialects of Unix-like OSes, though. A setpgrp call (in the callback
which you can request Popen to perform, after it forks and before it
execs) might suffice... except that you can't rely on children process
not to setpgrp's themselves, can you?!


Alex
 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      10-17-2005
In article <1h4lkml.gx8kslqnnnflN%(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alex Martelli) wrote:
> Micah Elliott <(E-Mail Removed)> wrote:


[... re problem killing children of shell script ...]

> > Is there any way to enable Python's subprocess module to do (implicit?)
> > group setup to ease killing of all children? If not, is it a reasonable
> > RFE?

>
> Not as far as I know. It might be a reasonable request in suitable
> dialects of Unix-like OSes, though. A setpgrp call (in the callback
> which you can request Popen to perform, after it forks and before it
> execs) might suffice... except that you can't rely on children process
> not to setpgrp's themselves, can you?!


I bet that wouldn't be a problem, though. subprocess.Popen
constructor takes a preexec_fn parameter that looks like it
might be a suitable place to try this. (Interesting that
it's a function parameter, not a method to be overridden by
a subclass.)

Donn Cave, (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a python script as an exe and run a new process from it venutaurus539@gmail.com Python 9 02-27-2009 05:22 PM
WebService ignoring timeout properties? (Server was unable to process request. --> Timeout exired) Lars-Erik Aabech ASP .Net Web Services 3 04-29-2005 02:21 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM
web.config session timeout and forms authentication timeout Do ASP .Net 2 11-23-2003 02:27 PM
Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Bob Johnson ASP .Net 0 08-07-2003 12:52 PM



Advertisments