'Non-Blocking raw_input()
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)
Solution 1:[1]
You created a thread but forget to start it:
listener = threading.Thread(target=kbdListener)
listener.start()
Solution 2:[2]
In addition to MydKnight's answer (starting the thread), you need to change rawInput to raw_input, and it needs to be in some sort of while loop otherwise you'll only get one raw_input() logged.
Solution 3:[3]
I found the accepted answer didn't work for me - it would still block at raw_input even in a separate thread. However, when I switched the threads around it worked right away.
import threading
def mainWork():
while 1:
#whatever you wanted to do until an input is received
myThread = threading.Thread(target=mainWork)
myThread.start()
while 1:
input = raw_input()
#do stuff with input when it is received
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tuan Anh Hoang-Vu |
| Solution 2 | SiHa |
| Solution 3 | Hester |
