473,388 Members | 873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Timeout test hangs IDLE

I'm trying to integrate the timeout function from here
http://aspn.activestate.com/ASPN/Coo.../Recipe/473878 into a
long running automation script and the following code causes IDLE after
20 or 30 iterations in countTest.

This is in 2.5, XP and there's no traceback.

Could someone point me at the user error?

Thanks in advance.
def countTest():
for i in xrange(10000000):
print i
return True

def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None

def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default

it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result

def runTest():
timeout(countTest, timeout_duration=5)

if __name__ == "__main__":
runTest()
Dec 5 '07 #1
6 2074
On Dec 5, 6:00 am, "Andreas Tawn" <andreas.t...@ubisoft.comwrote:
I'm trying to integrate the timeout function from herehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473878into a
long running automation script and the following code causes IDLE after
20 or 30 iterations in countTest.

This is in 2.5, XP and there's no traceback.

Could someone point me at the user error?

Thanks in advance.

def countTest():
for i in xrange(10000000):
print i
return True

def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None

def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default

it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result

def runTest():
timeout(countTest, timeout_duration=5)

if __name__ == "__main__":
runTest()
I'm confused. What does it cause IDLE to do? I tried running the
script and it ran fine. I killed it 17346 since I have no intention of
letting it tie up my work for your extraordinary large iteration
number.

I'm using Python 2.4 on Windows XP.

Mike
Dec 5 '07 #2
On Dec 5, 6:00 am, "Andreas Tawn" <andreas.t...@ubisoft.comwrote:
I'm trying to integrate the timeout function from
herehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/47
3878into a
long running automation script and the following code
causes IDLE after
20 or 30 iterations in countTest.

This is in 2.5, XP and there's no traceback.

Could someone point me at the user error?

Thanks in advance.

def countTest():
for i in xrange(10000000):
print i
return True

def timeout(func, args=(), kwargs={}, timeout_duration=1,
default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None

def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default

it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result

def runTest():
timeout(countTest, timeout_duration=5)

if __name__ == "__main__":
runTest()
I'm confused. What does it cause IDLE to do? I tried running the
script and it ran fine. I killed it 17346 since I have no intention of
letting it tie up my work for your extraordinary large iteration
number.

I'm using Python 2.4 on Windows XP.

Mike
Sorry, I need a better proof-reader.

When I run that code, the output gets to ~26 and then IDLE (or the
shell, I'm not sure which) hangs and there's zero CPU activity in the
pythonw.exe process.
Dec 5 '07 #3
On Dec 5, 6:00 am, "Andreas Tawn" <andreas.t...@ubisoft.comwrote:
I'm trying to integrate the timeout function from
herehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/47
3878into a
long running automation script and the following code
causes IDLE after
20 or 30 iterations in countTest.
>
This is in 2.5, XP and there's no traceback.
>
Could someone point me at the user error?
>
Thanks in advance.
>
def countTest():
for i in xrange(10000000):
print i
return True
>
def timeout(func, args=(), kwargs={}, timeout_duration=1,
default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
>
def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default
>
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result
>
def runTest():
timeout(countTest, timeout_duration=5)
>
if __name__ == "__main__":
runTest()
I'm confused. What does it cause IDLE to do? I tried running the
script and it ran fine. I killed it 17346 since I have no
intention of
letting it tie up my work for your extraordinary large iteration
number.

I'm using Python 2.4 on Windows XP.

Mike
Sorry, I need a better proof-reader.

When I run that code, the output gets to ~26 and then IDLE (or the
shell, I'm not sure which) hangs and there's zero CPU activity in the
pythonw.exe process.
Also, I should say that the the idea is that the huge iteration should
timeout after 5 seconds. I only gave it a 10000000 range because I
didn't know how fast it would get through the loop. Perhaps you stopped
it before the timeout triggered?

Cheers,

Drea
Dec 5 '07 #4
On Dec 5, 10:37 am, "Andreas Tawn" <andreas.t...@ubisoft.comwrote:
On Dec 5, 6:00 am, "Andreas Tawn" <andreas.t...@ubisoft.comwrote:
I'm trying to integrate the timeout function from
herehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/47
3878into a
long running automation script and the following code
causes IDLE after
20 or 30 iterations in countTest.
This is in 2.5, XP and there's no traceback.
Could someone point me at the user error?
Thanks in advance.
def countTest():
for i in xrange(10000000):
print i
return True
def timeout(func, args=(), kwargs={}, timeout_duration=1,
default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result
def runTest():
timeout(countTest, timeout_duration=5)
if __name__ == "__main__":
runTest()
I'm confused. What does it cause IDLE to do? I tried running the
script and it ran fine. I killed it 17346 since I have no
intention of
letting it tie up my work for your extraordinary large iteration
number.
I'm using Python 2.4 on Windows XP.
Mike
Sorry, I need a better proof-reader.
When I run that code, the output gets to ~26 and then IDLE (or the
shell, I'm not sure which) hangs and there's zero CPU activity in the
pythonw.exe process.

Also, I should say that the the idea is that the huge iteration should
timeout after 5 seconds. I only gave it a 10000000 range because I
didn't know how fast it would get through the loop. Perhaps you stopped
it before the timeout triggered?

Cheers,

Drea
No...I ran it for at least 30-60 seconds. Making a thread stop on
command is a pain though.

This link taught me one way to do it, but I cannot vouch how will it
works as I've never aborted any of my threads:

http://wiki.wxpython.org/LongRunningTasks

Mike
Dec 5 '07 #5
I once made a small app that used threads on IDLE.

There was a strange error when using 'print' & threads. When
what I printed filled the entire screen, instead of moving
all the text up, IDLE just hanged. Try running your code from
the shell instead, to see if the problem is in IDLE.

HTH,
Sergio
It looks like there's two issues here, an IDLE problem and a "me"
problem ;o)

I changed the countTest function to time.sleep(10) rather than a big
loop. When I run the script from the command line, the timeout triggers
properly after 5 seconds but the countTest function (running in a
seperate thread now) continues for the whole 10 seconds. This is the
"me" problem. I though that the timeout function would kill the
countTest thread, but it seems like it doesn't do that. Rather, the
timout just returns after 5 seconds with the message that the countTest
thread is still alive. I guess I need to set event flags so the
countTest function can end itself rather than trying to kill it from the
main thread?

When I run it from IDLE, everything runs correctly, but after the
countTest thread ends after 10 seconds, IDLE hangs as before. IDLE bug,
maybe?

Thanks for the help everyone.

The code looks like this now.

def countTest():# Succeeds from the command line, hangs in IDLE
import time
time.sleep(10)
return True

def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None

def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default

it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result

def runTest():
timeout(countTest, timeout_duration=5)
print "finished"

if __name__ == "__main__":
runTest()
Dec 6 '07 #6
En Thu, 06 Dec 2007 08:03:49 -0300, Andreas Tawn
<an**********@ubisoft.comescribió:
>There was a strange error when using 'print' & threads. When
what I printed filled the entire screen, instead of moving
all the text up, IDLE just hanged. Try running your code from
the shell instead, to see if the problem is in IDLE.

It looks like there's two issues here, an IDLE problem and a "me"
problem ;o)

I changed the countTest function to time.sleep(10) rather than a big
loop. When I run the script from the command line, the timeout triggers
properly after 5 seconds but the countTest function (running in a
seperate thread now) continues for the whole 10 seconds. This is the
"me" problem. I though that the timeout function would kill the
countTest thread, but it seems like it doesn't do that. Rather, the
timout just returns after 5 seconds with the message that the countTest
thread is still alive. I guess I need to set event flags so the
countTest function can end itself rather than trying to kill it from the
main thread?
It's not "your" problem, the cookbook recipe your code is based on should
make it clear: it does *not* kill the thread, it just returns to the
caller earlier (at most when the timeout elapses). The thread continues to
run (and consuming resources, of course).
If you actually want to stop a background task, the safest way would be
for the thread to test some condition periodically and gracefully stop
when requested. If that's not possible, and you have to forcibly kill a
running thread, there is a different recipe at
http://aspn.activestate.com/ASPN/Coo.../Recipe/496960 (follow the
link at the end for an updated version).
When I run it from IDLE, everything runs correctly, but after the
countTest thread ends after 10 seconds, IDLE hangs as before. IDLE bug,
maybe?
Not exactly; doing background tasks with a GUI is tricky and requires some
kind of cooperation, else the interfase usually becomes unresponsive.
How to run background tasks depends on the GUI toolkit used. IDLE uses
Tkinter, do you use that toolkit for your final program?

--
Gabriel Genellina

Dec 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: P. Glassel | last post by:
I'm having problems getting session timeouts to change programmaticlaly under IIS6.0. This is unchanged code that ran as expected under IIS5.0. Anyone else run into this problem? Thx.
1
by: Ryan P. Kennedy | last post by:
Hello: I have an application (using ADO 2.7 - written in VB) that access a SQL Server 2000 Database. Every once in a while, the program hangs when I am accessing the DB, and sometimes I get a...
1
by: Gordon Smith | last post by:
I'm just getting up to speed on Application Pools in IIS6 and I'm wondering if there would be any good reason to stick with the default value of for the Idle Timeout value for Worker processes...
5
by: Ed | last post by:
I have some questions in regards to Session variables and IIS Recycling: 1. Does the IIS setting 'Shutdown worker process after being idle' affect an application's session variables? Or is IIS...
5
by: John Nagle | last post by:
I thought I had all the timeout problems with urllib worked around, but no. socket.setdefaulttimeout is useful, but not always effective. I'm setting that to 15 seconds. If the host end won't...
10
by: Atul Shukla | last post by:
Hi, How can I avoid application timeout? Generally a web application time out is 20 minutes, however, we can define this timeout in web.config to any number of minutes. After giving 500 minutes of...
13
by: =?Utf-8?B?ZG91Zw==?= | last post by:
I support several intranet sites one of which had NO STATE. Even though content has ASP extensions, it was all rendered HTML. Site had NO session or application variables being managed. Site had...
4
by: Giampaolo Rodola' | last post by:
Hi there. We're talking about an asyncore-based server. Just for the heck of it I'd like to set a timeout which will disconnects the clients if they're inactive (i.e., no command or data transfer...
0
by: gnewsgroup | last post by:
I think I understand the difference between forms authentication timeout and the sessionstate timeout. That said, they are giving me headache. I have a simple test application using Forms...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.