title: Using a timer in a while true loop in Python
body: <p>I have a problem in managing a infinite while loop in Python, in which I would insert a timer as a sort of "watchdog". I try to explain better: the script has to listen on a serial channel and wait for messages coming from sensors connected on the other side of the channel. 
  I do this with a while True loop because the script must catch all the signals that are passing. However, even if it's probably unnecessary, i would like to insert a timer which is always reset every loop. So, if (for example) the loop get stuck for some reason, the timer will end and will exit the program. I thought i could do this in this way:</p>
  
  <pre><code>def periodicUpdate():
      exitTimer = threading.Timer(60.0, sys.exit())
      while True:
          exitTimer.start()
          readData()
          exitTimer.cancel()
  </code></pre>
  
  <p>Now, the problem is that when i start the script it immediatly exit. It seems that it reads <code>sys.exit()</code> before all the rest, and it doesn't respect the construct of the timer, but simply when periodicUpdate is called it exits. Why this happen?! I tried changing the syntax, putting sys.exit in an other function and call that function, i tried other solutions but always it behave in two ways: it exits or it behave as the timer doesn't exists. Can someone help me? Thanks very much</p>