title: Python, Raspberry pi, call a task every 10 milliseconds precisely
body: <p>I'm currently trying to have a function called every 10ms to acquire data from a sensor.</p>
  <p>Basically I was triggering the callback from a <code>gpio</code> interrupt but I changed my sensor and the one I'm currently using doesn't have a INT pin to drive the callback.</p>
  <p>So my goal is to have the same behavior but with an internal interrupt generated by a timer.</p>
  <p>I tried this from this <a href="https://stackoverflow.com/questions/11488877/periodically-execute-function-in-thread-in-real-time-every-n-seconds">topic</a></p>
  <pre><code>import threading
  
  def work (): 
    threading.Timer(0.25, work).start ()
    print(time.time())
    print &quot;stackoverflow&quot;
  
  work ()
  </code></pre>
  <p>But when I run it I can see that the timer is not really precise and it's deviating over time as you can see.</p>
  <pre><code>1494418413.1584847
  stackoverflow
  1494418413.1686869
  stackoverflow
  1494418413.1788757
  stackoverflow
  1494418413.1890721
  stackoverflow
  1494418413.1992736
  stackoverflow
  1494418413.2094712
  stackoverflow
  1494418413.2196639
  stackoverflow
  1494418413.2298684
  stackoverflow
  1494418413.2400634
  stackoverflow
  1494418413.2502584
  stackoverflow
  1494418413.2604961
  stackoverflow
  1494418413.270702
  stackoverflow
  1494418413.2808678
  stackoverflow
  1494418413.2910736
  stackoverflow
  1494418413.301277
  stackoverflow
  </code></pre>
  <p>So the timer is deviating by 0.2 milliseconds every 10 milliseconds which is quite a big bias after few seconds.</p>
  <p>I know that python is not really made for &quot;real-time&quot; but I think there should be a way to do it.</p>
  <p>If someone already have to handle time constraints with python I would be glad to have some advices.</p>
  <p>Thanks.</p>