On 27May2015 16:42, boB Stepp <robertvst...@gmail.com> wrote:
Python 2.4.4, Solaris 10
I realize that this is a bit old, but I got severely sidetracked! ~(:>)
[...]
On Mon, Apr 20, 2015 at 2:10 AM, Alan Gauld <alan.ga...@btinternet.com> wrote:
I would suggest forgetting about windows and think about
the processes that create them. Use the OS tools (via
the os module) to see if the first process is still running.
If so kill the process - which will in turn kill the window.

Is this a reasonable way to determine if a given pid is still active?
I found this at:

http://stackoverflow.com/questions/568271/how-to-check-if-there-exists-a-process-with-a-given-pid

import os

def check_pid(pid):
   """ Check For the existence of a unix pid. """
   try:
       os.kill(pid, 0)
   except OSError:
       return False
   else:
       return True

I realize that this will also return True if I don't have permissions
to kill, or some such.

Yes it is reasonable and normal. However the clean way to do it involves an extra test:

    try:
        os.kill(pid, 0)
    except except OSError as e:
        if e.errno == errno.EPERM:
          return True
        return False
    else:
        return True

Do bear in mind that process ids repeat. So on a busy system (or if your own program is very long lived and the original process exited a long time ago) you might be polling that pid as used by something unrelated.

However, if you started the process directly and it has not been wait()ed for, then the pid will not be available for reuse and you will be ok.

You can find the process based on its name or based on
its PID which you could store in a file somewhere
(like in /tmp?)

I can implement this, but thinking about this anew has led to a
concern. Hypothetically, if I check for the existence of a running
process based on this stored pid, what is the likelihood that the pid
will be reassigned to something other than one of my program's windows
left open?

As discussed above. Possible but unlikely. And if you are the creator of the original process you can control the situation.

Cheers,
Cameron Simpson <c...@zip.com.au>

Processes are like potatoes.    - NCR device driver manual
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to