Hello all,
I know why the following doesn't work ... I can't figure out how to make it
work like I want it to. Basically, I only want the loop computing
fibonacci numbers to run for approx 5 seconds. The timer fires, the
exception is raised, but it is not caught ... because the execution stack
for the function that raises isn't in the same thread as the try: except:.
How do you fix this? Is it better to do this without exceptions as the
control construct?
Regards,
Mark
PS I tried two positions of starting the thread ... unfortunately, lexical
scope doesn't affect the execution stack ... it's still in its own space, I
guess.
#!/usr/bin/env python
import threading
class TimeException(Exception):
def __init__(self, i):
self.msg = "Out of time"
def raiser():
print "raising"
raise TimeException
print "raised"
def fib(x):
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
#position 1
#t = threading.Timer(5.0, raiser)
#t.start()
try:
# position 2
t = threading.Timer(5.0, raiser)
t.start()
for i in range(1,10000000):
t = fib(i)
print i, ": ", t
except TimeException:
print "thread done"
except:
print "other exception" 2 4683
"Mark" <Ho********@yahoo.com> schrieb im Newsbeitrag
news:Bv*******************@nwrddc03.gnilink.net... Hello all,
I know why the following doesn't work ... I can't figure out how to make
it work like I want it to. Basically, I only want the loop computing fibonacci numbers to run for approx 5 seconds. The timer fires, the exception is raised, but it is not caught ... because the execution stack for the function that raises isn't in the same thread as the try: except:.
How do you fix this? Is it better to do this without exceptions as the control construct?
Regards, Mark
PS I tried two positions of starting the thread ... unfortunately, lexical scope doesn't affect the execution stack ... it's still in its own space,
I guess.
Hi Mark,
are you deliberately trying to break a butterfly on the wheel?
try this:
------ cut here --------
#!/usr/local/bin/python
import time
def fib(x):
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
i=1
t=time.time()
while(time.time()-t<5):
print "%d: %d" % (i, fib(i))
i += 1
-------- cut ---------
HTH
Ciao Ulrich
Mark fed this fish to the penguins on Tuesday 28 October 2003 08:08 am: How do you fix this? Is it better to do this without exceptions as the control construct?
Assuming you need a general case applicable to anything with a "work
task" that needs to return some sort of result after some specific time
period...
I'd drop the exception, and use a Queue. The timer expiration should
write the termination code to the queue, and the worker task should be
coded such that it regularly checks the queue (non-blocking) for data.
import threading
import Queue
termQ = Queue.Queue(0)
def terminator():
global termQ
termQ.put("Time's Up, You're Dead")
def fib(x):
if x < 0: return None
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
termTimer = threading.Timer(5.0, terminator)
termTimer.start()
for i in xrange(1,10000000):
try:
msg = termQ.get_nowait()
print msg
break
except:
print i, ": ", fib(i)
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Bestiaria Home Page: http://www.beastie.dm.net/ < Home Page: http://www.dm.net/~wulfraed/ < This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William Bub |
last post by:
Is there an accurate way to create a "stopwatch" good to 1/10 of a second?
I'm not sure if I should use the timer control, or some way to access the
computer timer. I found the following site...
|
by: Michael Evans |
last post by:
Anyone,
I'm looking for a way to setup a callback function in our VC++ .ne
2003 7.1 console application using a higher resolution timer. Th
multimedia timer is too low resolution at 1...
|
by: Jim Hammond |
last post by:
The following code tries to excute a function 10 seconds after Page_Load by using a Timer, but the callback never gets called.
private void Page_Load(object sender, System.EventArgs e)
{
...
|
by: 180Reef |
last post by:
I am new to using timers and am not sure which timer is best used for
the timer driven vb program described below. Also, if I should use one
master timer or a timer for each button (system resource...
|
by: Jon Slaughter |
last post by:
Is it possible to have a synchronous thread... actually a timer that is
beyond the 1khz/1ms resolution that .NET offers?
I want to poll the parallel port at rates beyond 1khz... about 10 to 100...
|
by: Peter Oliphant |
last post by:
Note that although this involves SAPI, it is more a question about Timers
and event handlers.
I wrote a Speech Recognize handler (SAPI), and put some code in it to enable
a Timer. It would not...
|
by: Andrew Wan |
last post by:
I found this excellent High Speed Timer (in Pascal). I compiled it
(using Turbo Pascal 7 and it runs fine):
http://www.sorucevap.com/bilisimteknolojisi/programcilik/pascal/ders.asp?207995
and...
|
by: ssecorp |
last post by:
I am not clear about the results here.
from timeit import Timer
import Decorators
def fib(n):
a, b = 1, 0
while n:
a, b, n = b, a+b, n-1
|
by: tvnaidu |
last post by:
How to calculate Prescalar and Modulus values for PIT (Programmable Interrupt Timer). I have microcontroller running at 60MHz. I am thinking of writing 1 milli sec ISR for PIT. I need to calculate...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |