473,770 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can Python kill a child process that keeps on running?

Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.

Thanks

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. If you want to write software,
or articles, or do testing or research for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the 3 digit number)
zenguy at shaw666 dot ca
May 1 '06 #1
10 3004
I. Myself wrote:
Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.

May 2 '06 #2
Serge Orlov wrote:
I. Myself wrote:
Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.

That is bad news. Thanks anyway; bad news is better than no news.

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. If you want to write software,
or articles, or do testing or research for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the 3 digit number)
zenguy at shaw666 dot ca
May 2 '06 #3
I. Myself wrote:
Serge Orlov wrote:
I. Myself wrote:

Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distributio n, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.


That is bad news. Thanks anyway; bad news is better than no news.

Note, however, taht ctypes is planned to be a part of the 2.5
distribution, so while there may not be a platform-independent way to
achieve your goals you will at leats be able to do so without external
extensions.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

May 2 '06 #4
I. Myself wrote:
Serge Orlov wrote:
I. Myself wrote:

Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distributio n, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.


That is bad news. Thanks anyway; bad news is better than no news.

Note, however, that ctypes is planned to be a part of the 2.5
distribution, so while there may not be a platform-independent way to
achieve your goals you will at leats be able to do so without external
extensions.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

May 2 '06 #5
I. Myself wrote:
Serge Orlov wrote:
I. Myself wrote:
Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition. I'm
using Windows 2000, but I want my program to be portable to linux.


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.

That is bad news. Thanks anyway; bad news is better than no news.


The good news is that I think you can work around it using only stock
2.4 modules. The idea is that you launch a separate watchdog process
and communicate with it using portable asynchronous channel (file
system or socket). Here is totally untested (not even passed the
compilation) code just to show the idea how to communicate
asynchronously over filesystem:
======= watchdog.py =========
import os, sys

timeout = int(sys.argv[1])
commdir = sys.argv[2]
worker_pid = int(sys.argv[3])
heart_beat = os.path.join(co mmdir, "heartbeat" )
work_is_done = os.path.join(co mmdir, "done")

while True:
if os.path.exists( work_is_done):
break
if os.path.exists( heartbeat):
os.kill(worker_ pid)
break
file(heart_beat , "w").close( )
time.sleep(time out)
=============== ============

======= work_consumer.p y ========

# launch worker process
# launch watchdog

def heart_beat():
try:
os.remove(heart _beat)
except OSError:
pass

def done():
file(heart_beat , "w").close( )
try:
while True:
data = worker.read()
heart_beat()
# process data
if done:
break
finally:
done()
=============== ==============

If you don't like so much file system activity, you can implement
asynchronous communications over local socket. It is also portable.

May 2 '06 #6
Serge Orlov wrote:
I. Myself wrote:
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32


Can't you use a Timer thread and then send the runaway process a signal?
Does Windows not have the equivalent of SIGHUP/SIGTERM/SIGKILL?

May 2 '06 #7
Edward Elliott wrote:
Serge Orlov wrote:
I. Myself wrote:
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?


On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32


Can't you use a Timer thread and then send the runaway process a signal?
Does Windows not have the equivalent of SIGHUP/SIGTERM/SIGKILL?


Windows doesn't have signals, so os.kill (that should rather be called
os.send_signal) is not implemented on Windows. Perhaps os module should
have os.terminate(pi d) that can be implemented on Windows.

You're right about timer thread, it's better, I didn't think of it
because of my lack of experience with threading module. The watchdog
process code in my second post is almost useless: it's complicated and
os.kill is not available on windows anyway :-( Time to rest.

May 2 '06 #8
Steve Holden wrote:
I. Myself wrote:
Serge Orlov wrote:
I. Myself wrote:
Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does Python have any way to kill it?

This is not hypothetical; I'm doing it now, and it's working pretty
well, but I would like to be able to handle this run-on condition.
I'm
using Windows 2000, but I want my program to be portable to linux.
On linux it's pretty easy to do, just setup alarm signal. On windows
it's not so trivial to the point you cannot do it using python.org
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.c om>
has some code to help you. Also take a look at buildbot sources
<http://buildbot.sf.net > that uses twisted. Buildbot has the same
problem as you have, it needs to kill run away or non-responding
processes.


That is bad news. Thanks anyway; bad news is better than no news.

Note, however, that ctypes is planned to be a part of the 2.5
distribution, so while there may not be a platform-independent way to
achieve your goals you will at leats be able to do so without external
extensions.

I'm an intermediate Python programmer. Can you explain to me how ctypes
will let me kill a child process?

Thanks,

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. If you want to write software,
or articles, or do testing or research for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the 3 digit number)
zenguy at shaw666 dot ca
May 2 '06 #9
Dennis Lee Bieber wrote:
On Tue, 02 May 2006 17:00:42 GMT, "I. Myself" <No*****@Spam.n one>
declaimed the following in comp.lang.pytho n:

I'm an intermediate Python programmer. Can you explain to me how ctypes
will let me kill a child process?

ctypes allows you to call functions within pretty much any DLL file
(whereas the ActiveState win32api module only calls some core functions)

Ok, I don't know which function to call in which .dll file, but perhaps
I can find out. Will ctypes will be a module in Python 2.5?

Thanks

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. If you want to write software,
or articles, or do testing or research for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the 3 digit number)
zenguy at shaw666 dot ca
May 4 '06 #10

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

Similar topics

4
8764
by: klappnase | last post by:
Hello, everyone, I am running python2.2.2 on a linux box. I want to call a shell command and get the PID of this child process so I have the possibility to abort the child process while it is still running. I tried the popen2 module for that: self.pp = popen2.Popen3(cmd)
6
7052
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with os.kill(self.popen.pid, signal.SIGTERM) Everything works fine on MacOSX. However, I'm doing a port to Solaris (so I can run it on my web site) and find that the child processes are not stopping! Solaris is creating TWO new processes: one for the SHELL...
2
1544
by: Tor Erik Sønvisen | last post by:
Hi. From my Python-program I spawn a new process. When using P_NOWAIT spawnl returns the pid but in windows it returns a process handle. Later I want to kill this process. How can I do this when I only have the process handle? -tores-
2
2060
by: Sarir Khamsi | last post by:
I would like to start a child process (like w/ popen3(), or some such) and then be able to interrupt it by sending it a control-c. How do I do that in Python? Is there a better way to gracefully halt a child process? Is there a more cross-platform way to start a child process (popen() is only on UNIX/Win32)? Thanks. Sarir
1
3478
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a single executable being run from the fork. The problem I am having now is that if I call a shell scripts, then lets say calls "xterm &", after the timeout has occurred, I kill the shell script, but the xterm is still running... I cannot seem to kill...
0
6132
by: lynn | last post by:
I'm doing a c# asp.net web application. It create a process "A" to excute an .exe file. This exe file is compiled from c++. In this exe file it will create another new process "B" to excute another .exe file. (This one is VB, but I think this doesn't matter). What I want to do is when I kill the first process "A" from c#(usill kill() function) I want to kill the child process "B" at the same time. If all is in c++ maybe I can figure it out...
1
1132
by: tcbrock | last post by:
I'm running Visual Studio 97 (vc++ 5.0) on Windows XP SP2 and when I run my program from inside the development environment, and then I quit it, it goes back to VC, but sometimes if I hit CTRL-ALT-DEL and look at my processes running, there is my program! Which can stop it from linking or running successfully until I kill it in there. Has anyone else seen this? It's just started happening to me in the last few months.
0
2190
by: WATYF | last post by:
This is my problem... I have some code that starts a Process and returns it to a variable... (prcBat) At any time while that process is running... I want to be able to Kill it by pressing a command button in the application. So the code goes something like this: psiBat = New ProcessStartInfo(arrRun.Item(0)(1)) psiBat.WindowStyle = ProcessWindowStyle.Hidden
4
4851
by: Ashit Vora | last post by:
Hi, My query is... I have a parent process which forks a child process to perform certain task. I wish to terminate the child process after certain amount of time (say 60 secs). Since I dont wish to leave any zombie process, I need to notify the Child that it has to terminate now. The parent can terminate only after all the child processes have
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.