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

Python / Windows process control

P: n/a
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
Jul 9 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.Popen (["notepad.exe"])
time.sleep (1)
for process in c.Win32_Process (ProcessId=notepad.pid):
process.Terminate ()

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

for process in c.Win32_Process (caption="notepad.exe"):
print process.ProcessId

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

</code>

HTH

TJG

[1] http://timgolden.me.uk/python/wmi.html
Jul 10 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.