How to let a loop run for a while before checking for break condition?
Fredrik Lundh
fredrik at pythonware.com
Tue Aug 29 12:51:38 EDT 2006
Sorin Schwimmer wrote:
> I am thinking on something in the following form:
>
> <code>
> import time
> import thread
>
> delay=True
>
> def fn()
> global delay
> time.sleep(<your_amount_of_time_in_seconds>)
> delay=False
>
> thread.start_new_thread(fn,())
>
> while delay:
> <statement 1>
> <statement 2>
> ...
if the loop calls out to python functions at least occasionally, you can
eliminate the flag by abusing the interpreter's stack check mechanism:
import time, thread, sys
def alarm():
time.sleep(1.234)
sys.setrecursionlimit(1)
thread.start_new_thread(alarm, ())
def do_some_work():
pass
t0 = time.time()
try:
while 1:
do_some_work()
except RuntimeError:
pass
print time.time() - t0
</F>
More information about the Python-list
mailing list