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

Terminating processes on Windows (handles and IDs)

Hi all,

I've always wondered why os.kill isn't supported on Windows. I found a
discussion somewhere from 2006 about this so it seems others have
wanted it, but still nothing. So I have a half-baked solution
involving calling "taskkill" on Windows Vista or "tskill" on Windows
XP via the shell. I feel there has to be a better way.

I'm also fairly confused about when I've got an ID and when I've got a
handle. The subprocess module gives me IDs which the above programs
accept, but other ways of spawning processes give me process handles
(while referring to them as process IDs in the docs...) and I don't
know how to kill a process with these. Besides, I've found an
amazingly useful PyGTK method, gobject.child_watch_add, which does
exactly what I want on UNIX but wants process handles on Windows. So I
can't use it in conjunction with subprocess there, and if I use some
other way of spawning processes I can't clean them up later.

Is there any way to convert one of these numbers to the other? Or to
get a process handle out of subprocess?
(There must be one down there somewhere, surely?)

Sorry for rambling a bit, am confused.

Regards,
Geoff Bache
Jun 27 '08 #1
4 2633
On Jun 23, 6:33*pm, geoffbache <geoff.ba...@jeppesen.comwrote:
Hi all,

I've always wondered why os.kill isn't supported on Windows. I found a
discussion somewhere from 2006 about this so it seems others have
wanted it, but still nothing. So I have a half-baked solution
involving calling "taskkill" on Windows Vista or "tskill" on Windows
XP via the shell. I feel there has to be a better way.

I'm also fairly confused about when I've got an ID and when I've got a
handle. The subprocess module gives me IDs which the above programs
accept, but other ways of spawning processes give me process handles
(while referring to them as process IDs in the docs...) and I don't
know how to kill a process with these. Besides, I've found an
amazingly useful PyGTK method, gobject.child_watch_add, which does
exactly what I want on UNIX but wants process handles on Windows. So I
can't use it in conjunction with subprocess there, and if I use some
other way of spawning processes I can't clean them up later.

Is there any way to convert one of these numbers to the other? Or to
get a process handle out of subprocess?
(There must be one down there somewhere, surely?)

Sorry for rambling a bit, am confused.

Regards,
Geoff Bache
My way to do it is using excellent wmi module by Tim Golden, which
relies on Mark Hammond's pywin32 and Windows native wmi functionality.
Here is the link - http://tgolden.sc.sabren.com/python/wmi.html
Maybe, there is a more elegant way of doing that, but it works for me,
and i feel nice with wmi.
Jun 27 '08 #2

Thanks for the tip. This does seem rather overkill to introduce all
these dependencies just to be able to
kill a process though...

I've discovered that subprocess.Popen objects have a member "_handle"
which is undocumented but
appears to work, so I'm using that for now. Better suggestions
gratefully received...

Geoff
My way to do it is using excellent wmi module by Tim Golden, which
relies on Mark Hammond's pywin32 and Windows native wmi functionality.
Here is the link -http://tgolden.sc.sabren.com/python/wmi.html
Maybe, there is a more elegant way of doing that, but it works for me,
and i feel nice with wmi.
Jun 27 '08 #3
Val-Amart wrote:
On Jun 23, 6:33 pm, geoffbache <geoff.ba...@jeppesen.comwrote:
>Hi all,

I've always wondered why os.kill isn't supported on Windows. I found a
discussion somewhere from 2006 about this so it seems others have
wanted it, but still nothing. So I have a half-baked solution
involving calling "taskkill" on Windows Vista or "tskill" on Windows
XP via the shell. I feel there has to be a better way.

I'm also fairly confused about when I've got an ID and when I've got a
handle. The subprocess module gives me IDs which the above programs
accept, but other ways of spawning processes give me process handles
(while referring to them as process IDs in the docs...) and I don't
know how to kill a process with these. Besides, I've found an
amazingly useful PyGTK method, gobject.child_watch_add, which does
exactly what I want on UNIX but wants process handles on Windows. So I
can't use it in conjunction with subprocess there, and if I use some
other way of spawning processes I can't clean them up later.

Is there any way to convert one of these numbers to the other? Or to
get a process handle out of subprocess?
(There must be one down there somewhere, surely?)

Sorry for rambling a bit, am confused.

Regards,
Geoff Bache

My way to do it is using excellent wmi module by Tim Golden, which
relies on Mark Hammond's pywin32 and Windows native wmi functionality.
Here is the link - http://tgolden.sc.sabren.com/python/wmi.html
Maybe, there is a more elegant way of doing that, but it works for me,
and i feel nice with wmi.
While I'm always happy to see WMI promoted <grinthis is one of
these occasions when there *are* other solutions. A few points to respond
to the OP:

1) In the trunk versions of Python (2.6 & 3.0, both in beta), the subprocess.Popen
objects have grown a .kill method:

<dump>

Python 2.6b1+ (trunk:64424, Jun 20 2008, 15:32:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>import subprocess
p = subprocess.Popen (["notepad.exe"])
p.kill ()
</dump>
so maybe some of the problem has gone away in any case.

2) The Popen objects have an internal _handle attribute which,
prior to 2.6, is used to pass along to the TerminateProcess
function of the Windows API. Here's a recipe illustrating
various techniques:

http://aspn.activestate.com/ASPN/Coo.../Recipe/347462
3) Under the covers, subprocess calls the CreateProcess Windows API:

http://msdn.microsoft.com/en-us/libr...25(VS.85).aspx

This passes back out four params: the process handle, the thread
handle, the process id and the thread id. The process handle is
kept in the _handle attribute of the Popen object; the process id
is kept in the pid attribute. You can pass whichever of these makes
sense to other routines which require them.

4) (In case it helps). The getpid function of the OS works perfectly
well under windows to return the Process Id of the current process
(*not* the handle).

5) If you *do* use WMI, then be aware that the Win32_Process object
has two likely-looking attributes: Handle and ProcessId. They *both*
contain the process id.

TJG
Jun 27 '08 #4

Thanks for the help Tim!

Good to see this is being sorted in Python at last, although it'll be
some time
before I can use only Python 2.6 I suspect...

I'm making use of _handle now and it works - most of the time.
The remaining issues are probably PyGTK problems rather than python
ones though,
and hence off topic here.

Regards,
Geoff
Jun 27 '08 #5

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

Similar topics

1
by: Guy | last post by:
Hi I've done some projects in python for my work which invole some test scripts etc. All quite basic stuff, I find python a good language to work in but have come up against a brick wall. I'm...
4
by: Pif Paf | last post by:
I am writing programs that will run as TCP servers. Briefly, I want to set up a TCP server on a port in such a way that if another server is already sitting on that port (both servers are Python...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
4
by: waltborders | last post by:
Hi, Because the blind are unable to use a mouse, keyboard navigation is key. A major difficulty is that not all windows forms controls are keyboard 'tab-able' or 'arrow-able' or have "tab...
23
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
3
by: Wizou | last post by:
i'm using .NET 2.0, and i've made a lot of tests i've come to the conclusion that TCP servers (tcplistener), started by a father process, are somewhat inherited by child processes if using...
1
by: Dave | last post by:
I'm using a small vb.net app to open up applications within a Citrix/terminal services session. Under some conditions, I'd like the app to close those applications. However, code I've found online...
0
markryan57
by: markryan57 | last post by:
Greetings, I have seen different ideas on how to terminate active processes (i.e., MS Access, Excel, etc) from VB6 - what is the most effecient method of finding and terminating active processes? ...
2
pablojoshua
by: pablojoshua | last post by:
Ok I have the current processes showing, but it shows the path of the process too. I just want the list to show the .exe or .dll name with the suffix. This is what I have so far (from microsoft) ...
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: 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
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,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.