title: Python: Call function at fixed rate
body: <p>The function <em>do_something</em> should be called at fixed rate (e.g. 10 seconds).</p>
  
  <p>I found links such as <a href="https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python">What is the best way to repeatedly execute a function every x seconds in Python</a> or <a href="https://stackoverflow.com/questions/8600161/executing-periodic-actions-in-python">Executing periodic actions in Python</a>. Unfortently those implementing gave the same resoluts like i would have had </p>
  
  <pre><code>time.sleep(10)
  </code></pre>
  
  <p>at the end of the <em>while</em>-loop, which is not the behavior i am seeking for.</p>
  
  <p>Hier is an example output:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>where the wish behavior should have been:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>At present this is my solution:</p>
  
  <pre><code>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 )
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>PS
  This is a small program.</p>