multithreading - Trouble getting variable read out of a thread in python -
so i'm trying write program looks keyboard presses , in main program based upon user inputs. i'm trying run keyboard listening in thread , compare whats in variable in main loop, don't ever seem getting threaded keyboard input. in below code, print maybe updating line never happens, else block main while loop. need main loop aware of keys pressed 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 keyboard input value" playingid = kbdinput else: print "no input keyboard detected. sleeping 2 seconds" time.sleep(2)
you need start listener calling listener.start()
after creating listener
.
listener = threading.thread(target=kbdlistener) listener.start()
Comments
Post a Comment