Non-Blocking raw_input() in python -


after digging around in while, still haven't found answer hope common need. need main thread "stuff" until receives input , act on input, return original "stuff". problem every time seems program execution seems halt @ raw input, whether call in thread or anywhere else. forwarning i'm pretty novice python, i'd hope shouldn't nasty implement. here i'm playing (pulled other question threading question answered handily)

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) 

if want keep while loop going on forever, need create new thread , start it, each time old 1 has finished.

i updated example in question make work:

import threading import time  kbdinput = '' playingid = '' finished = true  def kbdlistener():     global kbdinput, finished     kbdinput = raw_input("> ")     print "maybe updating...the kbdinput variable is: {}".format(kbdinput)     finished = true  while true:     print "kbdinput: {}".format(kbdinput)     print "playingid: {}".format(playingid)     if playingid != kbdinput:         print "received new keyboard input. setting playing id keyboard input value"         playingid = kbdinput     else:         print "no input keyboard detected. sleeping 2 seconds"     if finished:         finished = false         listener = threading.thread(target=kbdlistener)         listener.start()     time.sleep(2) 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -