title: Python multithreading, global variables, and raw_input in Raspberry Pi
body: <p>I'm completely new in Python and RPi. I'll try to make my question and sample code as general as possible.</p>
  
  <p>I'm working on this implementation where I need three threads to communicate:</p>
  
  <p>The first thread just waits until either of two flags, global variables <code>flag1</code> and <code>flag2</code>, becomes <code>TRUE</code>.</p>
  
  <p>The second thread waits for an input from the user. If the user inputs "a", then flag remains <code>FALSE</code>. But if the user inputs any other string, <code>flag1</code> becomes <code>TRUE</code>.</p>
  
  <p>The third thread looks into a GPIO pin and sets <code>flag2</code> to <code>TRUE</code> when a push button is pressed.</p>
  
  <pre><code>import threading
  import time
  import RPI.GPIO as GPIO
  GPIO.setmode(GPIO.BOARD)
  GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) #set GPIO 11 to PULL-UP input
  
  flag1 = FALSE
  flag2 = FALSE
  
  def thread1():
      global flag1
      global flag2
  
      while 1:
  
          while flag1 == TRUE:
              print "flag1"
              flag1 = FALSE ##clear this flag
          while flag2 == TRUE:
              print "flag2"
              flag2 = FALSE ##clear this flag
          time.sleep(0.0001)
  
  def button():
      global flag1
  
      if (GPIO.input(11) == True): #button is NOT pressed
          flag1 = FALSE
      else:
          flag2 = TRUE
  
      time.sleep(0.0001)
  
  def input():
      global flag2
      x = "a"
  
      while (flag1 == FALSE) and (flag2 == FALSE):        
          y = raw_input("input: ")
  
          if x == y:
              print "input is a"
              flag1 = FALSE
          else:
              print "input is not a"
              flag1 = TRUE
  
  t1 = threading.Thread(target = thread1)
  t2 = threading.Thread(target = button)
  t3 = threading.Thread(target = input)
  t1.start()
  t2.start()
  t3.start()
  while True:
       time.sleep(0.0001)
  </code></pre>
  
  <p>Before coming up with this, I coded the threads independently and they work fine. But I'm having trouble now in combining them in one program and making them communicate. My problem is I think due to the sequential nature of the <code>raw_input()</code> function. Is there an alternative to this? I'm using Python 2.7.3 if that helps. Any tips or "programming optimization" would also be appreciated. Thank you!</p>
  
  <p>PS: My hardware for the input emulates a keyboard with carriage return.</p>