[Tutor] I may have solved my problem with killing tasks in Windows

Ian Campbell ian-campbell at shaw.ca
Sat Apr 25 21:49:57 CEST 2009


I am trying to use this  so maybe  no one has to reply... sorry to  
inconvenience  anyone.
I got it from Google  but  did not copy his name and cannot give my 
thanks to original author,  sorry.

import subprocess
from win32event import WaitForSingleObject, WAIT_TIMEOUT
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 it.
    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:\\BuyLiveHit.ahk"], 5000)
print "ret=",ret




More information about the Tutor mailing list