Detect hung apps spawned from python

David Stubbs david at snsys.com
Tue Jun 3 08:42:46 EDT 2003


Thanks for your help. I decided to go the Win32 way. Here's my solution...

---
PROCESS_STILL_RUNNING = 259
def run_script(test_dir, timeout = 2):
    timeout = timeout * 60 # Convert the timeout param from minutes to
seconds
    os.chdir(test_dir)
    pinfo = win32process.CreateProcess(
        None, 'python.exe "' + test_dir + '\\test.py"', None, None, 0, 0,
        None, None, win32process.STARTUPINFO())

    retval = PROCESS_STILL_RUNNING
    while retval == PROCESS_STILL_RUNNING:
        time.sleep(0.25)
        timeout = timeout - 0.25
        if timeout <= 0:
            win32process.TerminateProcess(pinfo[0], 1)
            print "ERROR: Process timeout"
            break
        retval = win32process.GetExitCodeProcess(pinfo[0])
    os.chdir("..\\")
    return retval
---

Regards,
Dave Stubbs.

"David Stubbs" <david at snsys.com> wrote in message
news:3edb494b$0$12997$afc38c87 at news.easynet.co.uk...
> Hi,
>
> I have a python app that builds a list of all subdirectories from the
> current directory, and then in turn runs a 'test.py' script from within
each
> of these directories. I currently do this using os.system(). Is there any
> easy way of detecting apps that crashed, and terminating them if so?
Ideally
> being able to set a time-out to wait for the application to finish would
be
> good.
>
> I think I could do it using CreateProcess, but, well, it just seems like
> overkill and that there'll be a much simpler method somewhere.
>
> Thanks,
> Dave.
>
>






More information about the Python-list mailing list