FAQ
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.

Search Discussions

  • Tim Golden at Jan 15, 2006 at 11:58 am

    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
  • Iapain at Jan 22, 2006 at 5:43 pm
    Thanks Tim, Yeah win32api is working normally.



    From http Sun Jan 22 18:59:12 2006
    From: http (Paul Rubin)
    Date: 22 Jan 2006 09:59:12 -0800
    Subject: Some thougts on cartesian products
    References: <dqva12$gg4$1@online.de>
    <leJAf.139711$65.3950594@twister1.libero.it>
    <1137944129.386363.265070@f14g2000cwb.googlegroups.com>
    <dr0erp$nj4$1@online.de>
    <1h9k7px.8lnrol1q6f93nN%aleax@mail.comcast.net>
    Message-ID: <7xd5ik2lfz.fsf@ruckus.brouhaha.com>

    aleax at mail.comcast.net (Alex Martelli) writes:
    given length. You could get a 6/49 lotto tip with something like:
    choice(set(range(49)).powerset(6))
    And that would be better than the current random.sample(range(49),6) in
    WHAT ways, exactly...?
    I think the first one would be incorrect since it samples with
    replacement. At least, it looks like it does.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 13, '06 at 7:14p
activeJan 22, '06 at 5:43p
posts3
users2
websitepython.org

2 users in discussion

Iapain: 2 posts Tim Golden: 1 post

People

Translate

site design / logo © 2022 Grokbase