title: Asyncio loop.call_later timing accuracy
body: <ul>
  <li>I am trying to have a function run periodically on the event loop</li>
  <li>It is vital for the function to be called at the rate specified</li>
  <li>I'm recursively scheduling the function to run using <code>asyncio.call_later</code></li>
  <li>It appears that <code>asyncio.call_later</code> does not call the function with the correct delay</li>
  <li>The timer handle returned by <code>call_later</code> confirms that the delay is greater than expected</li>
  </ul>
  <p><strong>Why is the function not being called at the specified rate?</strong></p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  import time
  
  def print_fps():
      global last_time
  
      time_now = time.time()
      dt = time_now - last_time
      if dt &gt; 0:
          print(f&quot;fps: {round(1/dt, 3)} [{round(dt,3)}s], target: {fps} [{round(1/fps,3)}s]&quot;)
      else:
          print(&quot;dt = 0&quot;)
      last_time = time_now
  
  def loop_func():
      # print fps info
      print_fps()
  
      # schedule next iteration of loop
      loop.call_later(1/fps, loop_func)
  
  async def main():
      loop_func()
  
  last_time = time.time()
  fps = 60
  
  loop = asyncio.get_event_loop()
  loop.create_task(main())
  loop.run_forever()
  </code></pre>
  <p>Output:</p>
  <pre><code>fps: 31.446 [0.032s], target: 60 [0.017s]
  fps: 32.693 [0.031s], target: 60 [0.017s]
  fps: 32.172 [0.031s], target: 60 [0.017s]
  fps: 31.663 [0.032s], target: 60 [0.017s]
  fps: 32.001 [0.031s], target: 60 [0.017s]
  fps: 30.549 [0.033s], target: 60 [0.017s]
  fps: 34.486 [0.029s], target: 60 [0.017s]
  fps: 31.366 [0.032s], target: 60 [0.017s]
  fps: 31.743 [0.032s], target: 60 [0.017s]
  fps: 30.057 [0.033s], target: 60 [0.017s]
  </code></pre>
  <p><a href="https://i.stack.imgur.com/MVX6m.png" rel="nofollow noreferrer">cProfile reveals nothing is blocking the event loop</a></p>