I am trying to accomplish a way to spawn a thread that waits for user input; if no input is entered within 10 seconds, I want the script to kill off the spawned thread and continue processing. I've got a way to get the input back from the thread if text is entered but I have no way to let the timeout kill off the newly spawned thread.
In the example below is the closest I have come. I tell the newly created thread that it is a daemon and it will exit when the main script exits. The issue that I have with this is that the thread will continue to wait until either the script exits, or the user has inputted something.
shared_var = ['1']
def run(ref):
ref[0] = raw_input("enter something: ")
print "shared var changed to '%s'" % (ref[0])
thread = threading.Thread(target=run, args=(shared_var,))
thread.daemon = True
thread.start()
time.sleep(10) # simplified timeout
#Need some way to stop thread if no input has been entered
print "shared var = " + shared_var[0]
I know abruptly killing a thread isn't the best way to go (Related Link), but I don't know how to interrupt the new thread waiting on the raw_input