The function do_something should be called at fixed rate (e.g. 10 seconds).
I found links such as What is the best way to repeatedly execute a function every x seconds in Python or Executing periodic actions in Python. Unfortently those implementing gave the same resoluts like i would have had
time.sleep(10)
at the end of the while-loop, which is not the behavior i am seeking for.
Hier is an example output:
2015-01-01 13:00:00
2015-01-01 13:00:10
2015-01-01 13:00:21
2015-01-01 13:00:31
2015-01-01 13:00:42
where the wish behavior should have been:
2015-01-01 13:00:00
2015-01-01 13:00:10
2015-01-01 13:00:20
2015-01-01 13:00:30
2015-01-01 13:00:40
At present this is my solution:
import time
import datetime
def do_something():
print(datetime.now())
period = 10
while True:
do_something()
t = time.time()
time_to_sleep = period - ( t % period )
time.sleep( time_to_sleep )
Unfortently with this method and my raspberry pi 3 I am unable to achive a stable timestamps at a fixed rate. Here is an output example:
2015-01-01 13:00:00
2015-01-01 13:00:10
2015-01-01 13:00:23
2015-01-01 13:00:33
2015-01-01 13:00:40
Any ideas?
PS This is a small program.