title: How to make the threading be executed following a fequencey (say, every 3 sec) at a (relatively) accurate time considering function execution time?
body: <p>I want to run a function every 3 second, and I do have searched similar topics. However, I didn't find any solution that can indeed meet my requirement, and the key issue is that in these solutions, they do not consider the time of executing the function itself. Consider the following code.</p>
  <pre><code>import datetime as dt
  import time
  import threading
  
  def counting():
      global  num
      time_now = dt.datetime.now()
      if num &gt; 0:
          print(f'count: {num}, time now: {time_now}') 
          num -= 1
          t = threading.Timer(3.0, counting)
          t.start()
  
  num = 5
  counting()
  </code></pre>
  <p>This prints every 3.0 seconds. The main issue is that in a real case, in stead of <code>print(f'count: {num}, time now: {time_now}') </code>, I will call a function, say, <code>func1()</code>, which will take some time between 1 second and 2.5 seconds. Hence, the real interval time between two calls will be more than 3 seconds (about 4-5.5 seconds). How can I write it to be exactly (of course, very small error is allowed) every 3 seconds between two calls? Thanks!</p>