Can Python kill a child process that keeps on running?

I

I. Myself

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
 
S

Serge Orlov

I. Myself said:
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.
 
I

I. Myself

Serge said:
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
 
S

Steve Holden

I. Myself said:
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
 
S

Steve Holden

I. Myself said:
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
 
S

Serge Orlov

I. Myself said:
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.
 
E

Edward Elliott

Serge said:
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?
 
S

Serge Orlov

Edward said:
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.
 
I

I. Myself

Steve said:
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
 
D

Dennis Lee Bieber

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?

<heh> Bill Gates admitting that something might need to be killed
when running on Windows?

Killing processes on Windows is the equivalent of clicking on the
[x] button until the process either acknowledges and does a clean
shutdown, or the system pops up the "process not responding: [wait]
[kill]" window.

So one would have to find the process "window", post the exit
message to it (WM_CLOSE), wait some, check for the process again, and
only THEN using the TerminateProcess() call.

See: http://www.ddj.com/dept/windows/184416547
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

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)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
I

I. Myself

Dennis said:
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
 
R

Roger Upole

Using Pywin32 (obviously not cross platform):

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

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top