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

J Correia correia_jREMOVECAPS at hotmail.com
Mon Nov 14 20:22:31 EST 2005


"Peter Hansen" <peter at engcorp.com> wrote in message
news:rfSdnS_tldxq1-jeRVn-jA at powergate.ca...
> Fredrik Lundh wrote:
> > Daniel Crespo wrote:
> >>>>>>os.spawnl(os.P_NOWAIT, "c:/windows/notepad.exe")
> >>>1944
> >>
> >>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
> http://docs.python.org/lib/os-process.html 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....
>
> -Peter

Yep...
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462





More information about the Python-list mailing list