How to Run a Python Script in Regular Interval
Sep 26, 2022
The aim of this page📝 is to share an extremely simple way to run a script repeatedly in regular interval (e.g. 3 seconds) in python. What is needed is just a built-in time
module with its sleep method which
Suspend execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time.
Note that it is a best practice to share the unit of the interval in an extra variable. I am also capitalizing the binding defining the interval as per pep-8’s practice of writing constants in upper case.
CODE
>>> import time
>>> SLEEP_SECONDS = 3
>>> while True:
... print("hello world")
... time.sleep(SLEEP_SECONDS)
hello world
hello world
hello world
hello world
hello world
hello world