I have a function which gets an input from user using raw_input, and my requirement is to set a timeout for the user, in case user doesn't give any input in time, some other default value should be assumed.
To implement the same, I have created a new thread (t1) for the input function, and from my main thread I'm monitoring the completion of t1, if t1 doesn't complete in time, my main thread should terminate t1.
How do I achieve the same?
Please note that I have already tried using signal and select modules, but it did not work in jython. Even deleting the thread using threading.Thread._Thread__delete(t1) did not work.
def get_input():
return raw_input("Enter: ") # blocked until user enters
t1 = threading.Thread(target=get_input)
t1.start()
timeout = 10
count = 0
while count < timeout:
count += 1
if not t1.isAlive():
break
time.sleep(1)
else:
# how do I kill the t1 here?