Best way to determine if a certain PID is still running

  • Thread starter David Hirschfield
  • Start date
D

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.

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
 
P

Paul Rubin

David Hirschfield said:
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?
 
R

Roy Smith

David Hirschfield said:
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.
 
T

Thomas Guettler

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
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top