Terminating processes on Windows (handles and IDs)

Discussion in 'Python' started by geoffbache, Jun 23, 2008.

  1. geoffbache

    geoffbache Guest

    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
    geoffbache, Jun 23, 2008
    #1
    1. Advertising

  2. geoffbache

    Val-Amart Guest

    On Jun 23, 6:33 pm, geoffbache <> wrote:
    > 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.
    Val-Amart, Jun 23, 2008
    #2
    1. Advertising

  3. geoffbache

    geoffbache Guest

    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.
    geoffbache, Jun 24, 2008
    #3
  4. geoffbache

    Tim Golden Guest

    Val-Amart wrote:
    > On Jun 23, 6:33 pm, geoffbache <> wrote:
    >> 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 <grin> this 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/Cookbook/Python/Recipe/347462


    3) Under the covers, subprocess calls the CreateProcess Windows API:

    http://msdn.microsoft.com/en-us/library/ms682425(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
    Tim Golden, Jun 24, 2008
    #4
  5. geoffbache

    geoffbache Guest

    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
    geoffbache, Jun 25, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,086
    David Schwartz
    Dec 9, 2003
  2. Replies:
    2
    Views:
    695
    Beat Bolli
    Jun 21, 2005
  3. eino
    Replies:
    1
    Views:
    425
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    May 8, 2007
  4. Marc Heiler
    Replies:
    1
    Views:
    163
    Robert Klemme
    May 24, 2009
  5. Madhur
    Replies:
    8
    Views:
    190
    Tad McClellan
    Dec 25, 2003
Loading...

Share This Page