473,385 Members | 2,044 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,385 software developers and data experts.

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 2956
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.com>
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.com>
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
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.com>
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
distribution, you will need to poke in low level C API using win32
extensions or ctypes. AFAIK twisted package <http://twistedmatrix.com>
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.com>
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(commdir, "heartbeat")
work_is_done = os.path.join(commdir, "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(timeout)
===========================

======= work_consumer.py ========

# 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(pid) 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.com>
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.none>
declaimed the following in comp.lang.python:

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
Using Pywin32 (obviously not cross platform):

import win32api,win32con
h=win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, <pid>)
win32api.TerminateProcess(h, <exit code>)

Roger

"I. Myself" <No*****@Spam.none> wrote in message news:g0x5g.104639$7a.102977@pd7tw1no...
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


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
May 6 '06 #11

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

Similar topics

4
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...
6
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...
2
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...
2
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...
1
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...
0
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...
1
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...
0
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.