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

  • Thread starter =?ISO-8859-1?Q?Gerhard_H=E4ring?=
  • Start date
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Yves said:
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:
1944

HTH,

-- Gerhard
 
D

Daniel Crespo

Hi

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
 
F

Fredrik Lundh

Daniel said:
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:
1948

</F>
 
D

Daniel Crespo

import subprocess

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
 
D

Daniel Crespo

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

Peter Hansen

Fredrik said:
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
 
J

J Correia

Peter Hansen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top