473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Run process with timeout

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

Oct 17 '05 #1
5 7924
Natan <na*******@gmail.com> 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
Oct 17 '05 #2
P
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.
Oct 17 '05 #3
On Oct 17, Alex Martelli wrote:
Natan <na*******@gmail.com> 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
<md*@micah.elliott.name>
Oct 17 '05 #4
Micah Elliott <md*@micah.elliott.name> 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
Oct 17 '05 #5
In article <1h***************************@yahoo.com>,
al*****@yahoo.com (Alex Martelli) wrote:
Micah Elliott <md*@micah.elliott.name> 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, do**@u.washington.edu
Oct 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jacek Pop³awski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
4
by: Anders Borum | last post by:
Hello! I am working on improving my threading skills and came across a question. When working with the ReaderWriterLock class, I am getting an unhandled exception if I acquire a WriterLock with...
0
by: beachnut | last post by:
Hi, all. I am getting the following entry in the Event Log once or twice a day: "A worker process with process id of '2496' serving application pool 'DefaultAppPool' has requested a recycle...
2
by: Chris Langston | last post by:
I have a Web Server running IIS 5 or 6 on Windows 2K and Windows 2003 Server that is experiencing strange shutdown problems. We are using ASP.NET v1.1 and our application is written in VB.NET ...
2
by: Lars Netzel | last post by:
There's a setting in the IIS where I have set Enabled Session Timeout to 20 minutes Then there's a setting in the WebConfig file.. <sessionState mode="InProc"...
17
by: jensen bredal | last post by:
Hello, i'm struggling with a somehow badly understood session scenario. I provide acces to my pages based on form authentication using Session cookies. Som of my pages are supposed to be...
8
by: Thierry Lam | last post by:
I can use the python function raw_input() to read any input from the user but how can I add a timeout so that my program exits after x period of time when no input has been entered. Thierry
5
by: Ed | last post by:
I have some questions in regards to Session variables and IIS Recycling: 1. Does the IIS setting 'Shutdown worker process after being idle' affect an application's session variables? Or is IIS...
12
by: Sophie000 | last post by:
I am implementing a peer-to-peer program: When a computer A receives “get file XXX” from stdio, it broadcasts (floods) a request packet to ask others. Both Computer B and C have the file so they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.