By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Can Python kill a child process that keeps on running?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.