Timer interrupt of execution?

M

Mark

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"
 
U

Ulrich Petri

Mark said:
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
 
D

Dennis Lee Bieber

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)


--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top