469,290 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Best way to determine if a certain PID is still running

I'm launching a process via an os.spawnvp(os.P_NOWAIT,...) call.
So now I have the pid of the process, and I want a way to see if that
process is complete.

I don't want to block on os.waitpid(), I just want a quick way to see if
the process I started is finished. I could popen("ps -p %d" % pid) and
see whether it's there anymore...but since pids get reused, there's the
chance (however remote) that I'd get a false positive, plus I don't
really like the idea of calling something non-pure-python to find out.

So, should I run a monitor thread which just calls os.waitpid() and when
the thread indicates via an event that the process completed, I'm golden?

All suggestions welcome, looking for simple and clean over wickedly-clever,
-David

--
Presenting:
mediocre nebula.

Feb 3 '06 #1
3 3859
David Hirschfield <da****@ilm.com> writes:
So, should I run a monitor thread which just calls os.waitpid() and
when the thread indicates via an event that the process completed, I'm
golden?


Umm, what OS? And do you have any control over the program running in
the subprocess, or is it doing something arbitrary? I.e. I'm
wondering if can you do stuff like sharing a file descriptor with the
subprocess, or keeping a pipe open.

Using a monitor thread doesn't sound too bad, but there may be ways to
avoid it. But if you're willing to launch a thread, why not just
spawn there without NOWAIT?
Feb 3 '06 #2
David Hirschfield <da****@ilm.com> wrote:
I'm launching a process via an os.spawnvp(os.P_NOWAIT,...) call.
So now I have the pid of the process, and I want a way to see if that
process is complete.

I don't want to block on os.waitpid(), I just want a quick way to see if
the process I started is finished.
On Unix, you can do kill (pid, 0), and if you get back ESRCH, you know the
pid doesn't exist. It is the classic way to ask if a process is running on
Unix. But, there's several problems with that, the biggest one being that
those semantics don't seem to be exposed by phthon's os.kill() method.
So, should I run a monitor thread which just calls os.waitpid() and when
the thread indicates via an event that the process completed, I'm golden?


That's what I would do.
Feb 3 '06 #3
Am Thu, 02 Feb 2006 17:10:24 -0800 schrieb David Hirschfield:
I'm launching a process via an os.spawnvp(os.P_NOWAIT,...) call.
So now I have the pid of the process, and I want a way to see if that
process is complete.

I don't want to block on os.waitpid(), I just want a quick way to see if
the process I started is finished. I could popen("ps -p %d" % pid) and
see whether it's there anymore...but since pids get reused, there's the
chance (however remote) that I'd get a false positive, plus I don't
really like the idea of calling something non-pure-python to find out.


Hi,

at least on linux you can test this:
os.path.exists("/proc/%d" % mypid)

If you want to be sure that the pid is not reused, you
can look at: /proc/PID/cmdline

Maybe you can read the parent-pid in the proc directory. This should be
the pid if your script.

HTH,
Thomas

--
Thomas GŁttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Feb 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

131 posts views Thread by Peter Foti | last post: by
18 posts views Thread by Christopher W. Douglas | last post: by
10 posts views Thread by PJ6 | last post: by
21 posts views Thread by John Salerno | last post: by
7 posts views Thread by semedao | last post: by
29 posts views Thread by gs | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.