1

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.

Eagle
  • 3,362
  • 5
  • 34
  • 46
  • What is "takt"? – Daniel Roseman Jul 06 '17 at 12:41
  • @DanielRoseman he means "interval". – Błotosmętek Jul 06 '17 at 12:44
  • Did you try this solution https://stackoverflow.com/a/474570/6324080? – K. Kirsz Jul 06 '17 at 12:47
  • @DanielRoseman fixed, thanks. Takt was changed with rate, but interval is correct as well. – Eagle Jul 06 '17 at 12:47
  • Why are you doing `period - ( t % period )`? – cs95 Jul 06 '17 at 12:49
  • The issue here is that the sleep function does not take into account the time it takes for your function to execute. – Professor_Joykill Jul 06 '17 at 12:49
  • I linked to a Q&A I replied to a few months ago (using only standard python, no extra packages). The problem here is that indeed you're not clocking how much time the processing takes. – Jean-François Fabre Jul 06 '17 at 12:50
  • @K.Kirsz, i saw the first answer at the link you sent. I tried with sched and it did not work. I will try it with twisted package – Eagle Jul 06 '17 at 12:51
  • What is resolution of your `time.time` - seconds only or milliseconds? – Błotosmętek Jul 06 '17 at 12:51
  • @Błotosmętek return the time in seconds since the epoch as a floating point number. From https://docs.python.org/3/library/time.html – Eagle Jul 06 '17 at 12:53
  • "Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. " - but anyway the only thing I can think of is that your `do_something` function does a lot more than just printing a timestamp, and therefore the timestamp is delayed - try putting it at the very beginning of your function. – Błotosmętek Jul 06 '17 at 12:56
  • @cᴏʟᴅsᴘᴇᴇᴅ `period - ( t % period )` is the rest time to the next timestamp. – Eagle Jul 06 '17 at 14:02

0 Answers0