timeout a process

Discussion in 'Python' started by iapain, Jan 13, 2006.

  1. iapain

    iapain Guest

    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("mycpp.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.
     
    iapain, Jan 13, 2006
    #1
    1. Advertising

  2. iapain

    Tim Golden Guest

    iapain wrote:
    > 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("mycpp.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.


    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 WaitForSingleObject with the process handle and a timeout
    Use TerminateProcess 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.CreateProcess (
    None,
    "c:/winnt/system32/notepad.exe",
    None, None, 0, 0, None, None,
    win32process.STARTUPINFO ()
    )
    #
    # Wait for it to finish, but give up after n secs
    #
    result = win32event.WaitForSingleObject (
    hProcess,
    1000 * TIMEOUT_SECS
    )
    #
    # If it's timed out, kill it
    #
    if result == win32event.WAIT_TIMEOUT:
    win32process.TerminateProcess (hProcess, -1)
    print "Killed off"
    else:
    print "Died naturally"

    </code>

    HTH
    Tim
     
    Tim Golden, Jan 15, 2006
    #2
    1. Advertising

  3. iapain

    iapain Guest

    Thanks Tim, Yeah win32api is working normally.
     
    iapain, Jan 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,797
    Bob Johnson
    Aug 7, 2003
  2. Do
    Replies:
    2
    Views:
    6,402
  3. Guoqi Zheng
    Replies:
    4
    Views:
    13,072
    Guoqi Zheng
    Jun 3, 2004
  4. Lars-Erik Aabech
    Replies:
    3
    Views:
    409
    [MSFT]
    Apr 29, 2005
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,306
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page