Python / Windows process control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Salim Fadhley

    Python / Windows process control

    Does anybody know of a python module which can do process management
    on Windows? The sort of thing that we might usually do with
    taskmgr.exe or process explorer?

    For example:

    * Kill a process by ID
    * Find out which process ID is locking an object in the filesystem
    * Find out all the IDs of a particular .exe file
    * Find all the details of a currently running process (e.g. given an
    ID tell me which files it uses, niceness, runtime)

    Thanks!

    Sal
  • Tim Golden

    #2
    Re: Python / Windows process control

    Salim Fadhley wrote:
    Does anybody know of a python module which can do process management
    on Windows? The sort of thing that we might usually do with
    taskmgr.exe or process explorer?
    >
    For example:
    >
    * Kill a process by ID
    * Find out which process ID is locking an object in the filesystem
    * Find out all the IDs of a particular .exe file
    * Find all the details of a currently running process (e.g. given an
    ID tell me which files it uses, niceness, runtime)
    As far as I know, the closest you're going to come here is
    WMI [1]. It won't do everything you ask, though. I don't know
    how to find out which processes have a lock on a filesystem
    object.

    When you say "Find all the ids of a particular .exe file" I
    assume you mean: all the processes which were started
    by running that file.

    <code>
    import subprocess
    import time

    import wmi
    c = wmi.WMI ()

    #
    # Kill a process by id
    #
    notepad = subprocess.Pope n (["notepad.ex e"])
    time.sleep (1)
    for process in c.Win32_Process (ProcessId=note pad.pid):
    process.Termina te ()

    #
    # Which process ids correspond to an .exe
    #
    for i in range (5):
    subprocess.Pope n (["notepad.ex e"])

    for process in c.Win32_Process (caption="notep ad.exe"):
    print process.Process Id

    #
    # _Some_ (but not all) of the information about each file
    #
    for process in c.Win32_Process (caption="notep ad.exe"):
    print process
    process.Termina te ()

    </code>

    HTH

    TJG

    [1] http://timgolden.me.uk/python/wmi.html

    Comment

    Working...