how to start a process and get it's pid?

Peter Hansen peter at
Sat Nov 12 02:52:57 CET 2005

Fredrik Lundh wrote:
> Daniel Crespo wrote:
>>>>>>os.spawnl(os.P_NOWAIT, "c:/windows/notepad.exe")
>>I don't get the correct PID.
>>When I do os.spawnl(os.P_NOWAIT, "c:/windows/notepad.exe")
>>I get 168 (for example), while in the tasklist appears notepad.exe with
>>the 2476 PID.
> not sure, but the return value looks like a PID, so maybe you're seeing the
> PID for the cmd.exe instance used to run the program.  or something.

I believe it's documented here that the return value is not 
the PID but the "process handle".  I believe this can be converted to 
the PID with a convenient pywin32 call though at the moment I can't 
recall which.  Googling quickly suggests that 
win32process.GetWindowThreadProcessId(handle) will do the trick (the 
second item returned is the PID), but I'm fairly sure there's a simpler 
approach if you keep looking.  I recall there being a Cookbook recipe 
related to this too....


More information about the Python-list mailing list