I'll just get to the code to show you, I'm trying to stop my while loop when my timer is over. Is there a way to do that?
from threading import Timer
def run(timeout=30, runs):
timer(timeout)
while runs > 0 or over():
#Do something
print "start looping"
def timer(time):
t = Timer(time, over)
print "timer started"
t.start()
def over():
print "returned false"
return False
As you can see, I'm trying to use the function over()
to stop my while loop. Currently, when my time stop, over is returned as false, but it doesn't affect my while loop. I must be doing something wrong.
So what Timer
object allows me to do is to pass a function param so that when the timer is over, it calls that function, over()
. As over()
returns False
, it doesn't directly affect the while loop in real time. Will I need a callback to implicitly force the while
to be false or is my method of stopping is completly faulty and will not work.
Changes
over
is invoked, it will returnTrue
. Right now, I keep that in global state for simplicity. You will want to change this at some point.This returns the following after the 2 seconds have expired: