Timer interrupt of execution?
Ulrich Petri
ulope at gmx.de
Tue Oct 28 14:50:15 EST 2003
"Mark" <Hobbes2176 at yahoo.com> schrieb im Newsbeitrag
news:Bvwnb.13377$%e3.11891 at 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
More information about the Python-list
mailing list