title: Calling a threading.timer from a function and not seeing any output of the called thread in Python
body: <p>The idea is that I'm sending a message to an external device and setting a flag that tells the program that I'm waiting for a specific response.
  Until it waits for the response, the program will continue receiving other messages and evaluating them to know if they are a response that I requested.
  For this, I'm calling another function with a timer so I reset the flag after the configured timeout has expired. In other words, if such function executes and the flag is still marked as waiting for response, it means no response has been received, hence I show a message that a timeout has occurred.
  If the response arrives before the timer has expired, than I cancel the timer, process the response and reset the flag.</p>
  
  <p>Any messages that are arriving while the timer is counting, are processed without any problems.</p>
  
  <p>The issue that I'm having is that the second thread does execute and does end correctly, but I don't see any output from it.
  I can see it is running by calling the printer() function below after the timer has started. I see 2 threads until the 12 seconds has passed, then I see 1 thread again. The 2nd thread should print some messages (and in the full code it should generate events for an external application).
  But I don't see any prints from it, and I don't see the events that it should generate.
  I suspect of something related to scope, because I'm creating a thread from a function to execute another function. I really would like this.</p>
  
  <p>For simplicity, I've removed part of the code, but the program basically works like this:
  The processData function is called when any data is received from the serial port.
  The MySensorsGatewayWrite is called when I want to send any data through the serial port.</p>
  
  <p>Any help is very welcome since I'm stuck now.</p>
  
  <p>Thanks!</p>
  
  <pre><code>import threading
  from threading import Timer,Thread,Event
  
  global waitingforresponse
  waitingforresponse=0
  global t
  
  def processData2 (received):
      global waitingforresponse
      if waitingforresponse == 1:
          waitingforresponse=0    #reset for further use
          print "Response received: "+str(received)
      else:
          print "Normal message that will be processed"
  
  def MySensorsGatewayWrite2(sendMessage):
      global waitingforresponse
      print "Data to send: "+str(sendMessage)
      if sendMessage=="question":
          waitingforresponse = 1  #sets a flag so we can check it later
          timerstart()
      else:
          waitingforresponse = 0  #unsets a flag so we can check it later
  
  def timerstart():
      global t
      global waitingforresponse
      t = Timer(12,checktimeout)
      t.start()
      print "Starting to count the timer for "+str(12)+" seconds"
      threadsativas=threading.activeCount()
      print "Active Threads="
      print str(threadsativas)
  
  def checktimeout():
      global t
      global waitingforresponse
      t.cancel()
      print "Finished to count the timer for "+str(12)+" seconds"
      if waitingforresponse==1:   #if it is still 1 after the timer period (ie. flag has not been reset by receiving a response)
          print ("Didn't receive a response in time. Sensor down?")
      else:
          print ("Already received a response in time! Good!")
  
  def printer2():
      print 'ipsem lorem'
      Activethreads=threading.activeCount()
      print "Activethreads="
      print str(Activethreads)
  
  #simulates some message being sent and receiving a response
  printer2()
  MySensorsGatewayWrite2("question")
  printer2()
  processData2("Some response")
  printer2()
  
  #simulates some message being sent and NOT receiving a response
  printer2()
  MySensorsGatewayWrite2("question")
  printer2()
  </code></pre>