After digging around in SO for a while, I still haven't found a good answer to what I would hope is a fairly common need. Basically I need a main thread to do "stuff" until it receives input and then act on that input, then return to the original "stuff". My problem every time seems to be that my program execution seems to halt completely at the raw input, whether I call it in a thread or anywhere else. Forwarning I'm pretty novice to python, but I'd hope this shouldn't be too nasty to implement. Here is what I'm playing with (pulled from my other question where my threading question was answered handily)
So I'm trying to write a program that looks for keyboard presses and then does something in the main program based upon what the user inputs. I'm trying to run the keyboard listening in a thread and then compare whats in the variable in my main loop, but I don't ever seem to be getting the threaded keyboard input. In the below code, the print maybe updating line never happens, just the else block from the main while loop. What do i need to do so that my main loop is aware of the keys pressed by the user?
import threading
import time
kbdInput = ''
playingID = ''
def kbdListener():
global kbdInput
kbdInput = rawInput()
print "maybe updating...the kbdInput variable is: ",kbdInput
listener = threading.Thread(target=kbdListener)
while True:
print "kbdInput: ",kbdInput
print "playingID: ",playingID
if playingID != kbdInput:
print "Recieved new keyboard Input. Setting playing ID to keyboard input value"
playingID = kbdInput
else:
print "No input from keyboard detected. Sleeping 2 seconds"
time.sleep(2)