enumerating processes

Shane Geiger sgeiger at ncee.net
Tue Mar 27 18:53:58 CEST 2007


> I believe you are looking for os.getpid()
I apologize for providing that bit of incorrect info. 

It looks to me as if Python 1.5 had os.process which might have done 
what you wanted (although perhaps not in an OS-independent way).  I 
wonder why there isn't an OS-independent way to do that in Python now.

After having seen your message about tomcat.exe, I assume we are talking 
just about Windows.  ;-)  This looks like an excellent way to deal with 
processes on Windows:

# http://tgolden.sc.sabren.com/python/wmi_cookbook.html#running_processes

# List all running processes

import wmi
c = wmi.WMI ()
for process in c.Win32_Process ():
  print process.ProcessId, process.Name


# List all running notepad processes

import wmi
c = wmi.WMI ()
for process in c.Win32_Process (name="notepad.exe"):
  print process.ProcessId, process.Name


# Create and then destroy a new notepad process

import wmi
c = wmi.WMI ()
process_id, return_value = c.Win32_Process.Create 
(CommandLine="notepad.exe")
for process in c.Win32_Process (ProcessId=process_id):
  print process.ProcessId, process.Name

result = process.Terminate ()







>
> 李现民 wrote:
>> hi ,all
>>    any one knows how to enumerate the current running processes , or 
>> how to obtain a specific process by its name or process id. I know I 
>> can do this in many  programming languages , but how in python? any 
>> one know?
>>  Thanks for any guidance.
>>
>> -- 
>> li xianmin
>>
>>            lixianmin at gmail.com <mailto:lixianmin at gmail.com> 
>

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 309 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070327/59b26b9d/attachment.vcf>


More information about the Python-list mailing list