timeout a process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iapain

    #1

    timeout a process

    Hello,
    I am trying to build and infinite loop handler in python 2.4 on windows
    platform. The problem is that i want to create a process and forcely
    kill/timeout after 2 sec to handle infinite loop in a gcc complied exe
    on cygwin. something like below

    os.system("mycp p.exe") # this exe is compiled with g++ and having an
    infinite loop

    I wish to terminate this after 2 sec. I've tried Watchdog and deamon
    thread.. but nothing seem to work here.

  • Tim Golden

    #2
    Re: timeout a process

    iapain wrote:[color=blue]
    > Hello,
    > I am trying to build and infinite loop handler in python 2.4 on windows
    > platform. The problem is that i want to create a process and forcely
    > kill/timeout after 2 sec to handle infinite loop in a gcc complied exe
    > on cygwin. something like below
    >
    > os.system("mycp p.exe") # this exe is compiled with g++ and having an
    > infinite loop
    >
    > I wish to terminate this after 2 sec. I've tried Watchdog and deamon
    > thread.. but nothing seem to work here.[/color]

    I'm not 100% sure, but I think that the following approach will work:

    Use the win32process and win32event modules from the pywin32
    extensions.
    Use CreateProcess to run your .exe
    Use WaitForSingleOb ject with the process handle and a timeout
    Use TerminateProces s to kill your exe

    Something like this (tested only casually):

    <code>
    import win32process
    import win32event

    TIMEOUT_SECS = 2

    #
    # Do as little as possible to get a
    # process up and running.
    #
    hProcess, hThread, pid, tid = \
    win32process.Cr eateProcess (
    None,
    "c:/winnt/system32/notepad.exe",
    None, None, 0, 0, None, None,
    win32process.ST ARTUPINFO ()
    )
    #
    # Wait for it to finish, but give up after n secs
    #
    result = win32event.Wait ForSingleObject (
    hProcess,
    1000 * TIMEOUT_SECS
    )
    #
    # If it's timed out, kill it
    #
    if result == win32event.WAIT _TIMEOUT:
    win32process.Te rminateProcess (hProcess, -1)
    print "Killed off"
    else:
    print "Died naturally"

    </code>

    HTH
    Tim

    Comment

    • iapain

      #3
      Re: timeout a process

      Thanks Tim, Yeah win32api is working normally.

      Comment

      Working...