473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4005
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
2
by: RickPowell | last post by:
I wrote a VB .NET Service to restart the computer when certain business rules are triggered (such as not being rebooted in x days, no users logged on, time is 1-3 am, etc.). I am currently using...
10
by: PJ6 | last post by:
I've read some of the posts on this subject but let me add this little twist to the subject's question- I can appreciate the utility of doing screen caps off of other websites with PSP or PhotoShop...
4
by: Tom | last post by:
How can one determine if a .NET program is running in the developer IDE or running stand-alone? I am sure I saw something somewhere but I don't remember where or what. Thanks! Tom
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.