Equivalent of setInterval in python
Your solution looks fine to me.
There are several ways to communicate with threads. To order a thread to stop, you can use threading.Event()
, which has a wait()
method that you can use instead of time.sleep()
.
stop_event = threading.Event()
...
stop_event.wait(1.)
if stop_event.isSet():
return
...
For your thread to exit when the program is terminated, set its daemon
attribute to True
before calling start()
. This applies to Timer()
objects as well because they subclass threading.Thread
. See http://docs.python.org/library/threading.html#threading.Thread.daemon
Is it possible to create a function in Python similar to the JS interval?
import threading
def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"
printit()
the answer from: Run certain code every n seconds
how to use set_interval in python
You are passing in the result of calling doSomething()
. Pass in the function without calling it:
set_interval(doSomething, 5)
In Python, functions are just objects, just like strings or lists or integers. You can pass them around just the same.
You probably want to store the returned thread object, so you can later on stop the thread again:
t = set_interval(doSomething, 5)
# ...
t.stop()
Improve current implementation of a setInterval
To call a function repeatedly with interval
seconds between the calls and the ability to cancel future calls:
from threading import Event, Thread
def call_repeatedly(interval, func, *args):
stopped = Event()
def loop():
while not stopped.wait(interval): # the first call is in `interval` secs
func(*args)
Thread(target=loop).start()
return stopped.set
Example:
cancel_future_calls = call_repeatedly(60, print, "Hello, World")
# ...
cancel_future_calls()
Note: this version waits around interval
seconds after each call no matter how long func(*args)
takes. If metronome-like ticks are desired then the execution could be locked with a timer()
: stopped.wait(interval)
could be replaced with stopped.wait(interval - timer() % interval)
where timer()
defines the current time (it may be relative) in seconds e.g., time.time()
. See What is the best way to repeatedly execute a function every x seconds in Python?
How set a loop that repeats at a certain interval in python?
the following is a simplified version of @Mathias Ettinger's
Because call_at_interval
runs in a separate thread already, there is no need to use Timer
, since it would spawn a further thread.
Just sleep and call the callback directly.
from threading import Thread
from time import sleep
def call_at_interval(period, callback, args):
while True:
sleep(period)
callback(*args)
def setInterval(period, callback, *args):
Thread(target=call_at_interval, args=(period, callback, args)).start()
def hello(word):
print("hello", word)
setInterval(10, hello, 'world!')
Related Topics
How to Set Class Attributes from Variable Arguments (Kwargs) in Python
Convert Rgba Png to Rgb with Pil
Django Db Settings 'Improperly Configured' Error
How to Use Multiprocessing Queue in Python
Python Enum, When and Where to Use
Escape String Python for MySQL
Pandas Split Column into Multiple Columns by Comma
How Does Condensed Distance Matrix Work? (Pdist)
Pip Installing in Global Site-Packages Instead of Virtualenv
What Does a for Loop Within a List Do in Python
How to Redirect Stdout and Stderr to Logger in Python
Let JSON Object Accept Bytes or Let Urlopen Output Strings
How to Move a Tick Label in Matplotlib
How to Kill a Process on Windows from Within Python
"Importerror: No Module Named" When Trying to Run Python Script