Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > timeout a process

Reply
Thread Tools

timeout a process

 
 
iapain
Guest
Posts: n/a
 
      01-13-2006
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.

 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      01-15-2006
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

 
Reply With Quote
 
 
 
 
iapain
Guest
Posts: n/a
 
      01-22-2006
Thanks Tim, Yeah win32api is working normally.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WebService ignoring timeout properties? (Server was unable to process request. --> Timeout exired) Lars-Erik Aabech ASP .Net Web Services 3 04-29-2005 02:21 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM
Session contents lost despite Session.Timeout = 3000; and <sessionState mode="InProc" cookieless="false" timeout="300"> Carpe Diem ASP .Net 3 02-23-2004 07:10 PM
web.config session timeout and forms authentication timeout Do ASP .Net 2 11-23-2003 02:27 PM
Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Bob Johnson ASP .Net 0 08-07-2003 12:52 PM



Advertisments