waiting for user input in separate thread

Looks like there is no way to time user input. In the link that SmartElectron provided, the solution does not work since the timer is halted once the raw_input is requested.

Best solution so far is:

# Declare a mutable object so that it can be pass via reference
user_input = [None]

# spawn a new thread to wait for input 
def get_user_input(user_input_ref):
    user_input_ref[0] = raw_input("Give me some Information: ")

mythread = threading.Thread(target=get_user_input, args=(user_input,))
mythread.daemon = True
mythread.start()

for increment in range(1, 10):
    time.sleep(1)
    if user_input[0] is not None:
        break

Leave a Comment