Set timeout and kill external windows

gagsl-py2 at gagsl-py2 at
Tue Jun 19 17:42:49 CEST 2007

At Saturday 16/06/2007 02:24, you wrote:

> Thanks for your reply. I am new to Python and I
> seem to figure this out. I searched for examples
> on your recommendation below but couldn't do it.  I
> not familiar with subprocess and
> And I want to do this in Windows. Could you please
> write me the few lines I need to do this?

(Please keep posting on the list - other people may
help too, and I don't read this email too often).
Try this. (I had to use a private Popen attribute,

import subprocess
from win32event import WaitForSingleObject,
from win32api import TerminateProcess

def execute_and_wait(args, timeout=None):
    """Execute a command and wait until termination.
    If timeout elapses and still not finished, kill
    args: list containing program and arguments, or a
single string.
    timeout: maximum time to wait, in ms, or None.

    p = subprocess.Popen(args)
    if timeout:
        ret = WaitForSingleObject(p._handle, timeout)
        if ret==WAIT_TIMEOUT:
            TerminateProcess(p._handle, 1)
            return None
    return p.wait()

ret = execute_and_wait(["notepad",
"c:\\python\\LICENSE.txt"], 5000)
print "ret=",ret

Gabriel Genellina

Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the Python-list mailing list