python - How to manage real time input with periodic updates -
i have block @ position (0,0). periodically (say every 1 second) y-coordinate of block randomly updated +/-1.
and every time user inputs character (+/-) x-coordinate updated +/-1 user has inputted.
if x coord, create while loop, runs next iteration when input() gets value.
but how can deal both periodic update real time input (which can come @ time?)
threading friend:
import time threading import thread # defines thread handles console input class userinputthread(thread): def __init__ (self): thread.__init__(self) # process user input here: def run(self): while true: text = input("input: ") print("you said", text) # exit thread if text == "exit": return console_thread = userinputthread() console_thread.start() while true: time.sleep(5) print("function") # if thread dead, programme exit when current iteration of while loop ends. if not console_thread.is_alive(): break
userinputthread
runs in background , handles user input. print("function")
logic need in main thread.
Comments
Post a Comment