Tags: asgt, context, external, glodt, own, pid, process, programming, python, rose, wrotegt, yves

how to start a process and get its pid?

On Programmer » Python

5,051 words with 6 Comments; publish: Sun, 30 Dec 2007 23:27:00 GMT; (20062.50, « »)

Yves Glodt wrote:

> Hello,

> another question rose for me today...

> Is there a way to start an external process, in it's own context (not as

> the exec-() functions do), and get it's pid...? [...]

Check out the subprocess module if you're using Python 2.4.

Otherwise, you can always use os.spawn*, for example:

>>> os.spawnl(os.P_NOWAIT, "c:/windows/notepad.exe")

1944

HTH,

-- Gerhard

All Comments

Leave a comment...

  • 6 Comments
    • Hi

      > >>> 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.

      Why?

      Thanks

      Daniel

      #1; Sun, 30 Dec 2007 23:29:00 GMT
    • 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.

      > Why?

      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.

      try this instead:

      >>> import subprocess

      >>> p = subprocess.Popen("c:/windows/notepad.exe")

      >>> p.pid

      1948

      </F

      #2; Sun, 30 Dec 2007 23:30:00 GMT
    • > >>> import subprocess

      > >>> p = subprocess.Popen("c:/windows/notepad.exe")

      > >>> p.pid

      > 1948

      Yes, it works. But in my case, I need to run the program totally

      separated from my main program. So, when I start a new program through

      subprocess, it doesn't unlink. I mean, if I close my main app, so does

      the launched program. With startfile() it does the job, but I then I

      have to find what pid is through win32all module, etc.

      it would be very good if I can use spawnl

      Daniel

      #3; Sun, 30 Dec 2007 23:31:00 GMT
    • > 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.

      No. There wasn't a 196 PID for any of the processes.

      #4; Sun, 30 Dec 2007 23:32:00 GMT
    • 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

      #5; Sun, 30 Dec 2007 23:33:00 GMT
    • "Peter Hansen" <peter.python.todaysummary.com.engcorp.com> wrote in message

      news:rfSdnS_tldxq1-jeRVn-jA.python.todaysummary.com.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/Co...n/Recipe/347462

      #6; Sun, 30 Dec 2007 23:34:00 GMT