0

I am having trouble making a prompt that waits only for a certain amount of time for user input.

I've ended up using threading to start a thread that waits for the input and using the main thread to try and stop the program. The multithreading works just fine, both my raw_input code and my quit() code works perfectly fine...until I throw time.sleep() into the mix to delay the quitting of the program.

It seems like something with time.sleep() makes it wait for the thread to finish; however, I have not been able to find any time.sleep alternatives. I have not been able to find any raw_input alternatives either.

import threading 
import time

def ask():
    print raw_input("What's up?")

def giveUp():
    print "I give up!";
    quit();
    print "I already gave up!";
t1 = threading.Thread(target=ask);
t1.daemon = False;
t1.start()
time.sleep(0.1);
giveUp();

I expect the output "What's up" followed after a little bit by "I give up!" followed by the program quitting.

ColorCodin
  • 101
  • 2
  • 11
  • https://stackoverflow.com/questions/30929661/non-blocking-raw-input-in-python – Chris_Rands Jul 03 '19 at 15:28
  • Maybe it's just how Windows works the but "sleep" never ends – ColorCodin Jul 03 '19 at 15:46
  • you should find on internet special functon which checks if key was pressed but it doesn't wait for key. It can be called `keypress` or `getchar` or `getch`. Using this function you can create loop which checks time then it checks key and go back to beginning of this loop or exit loop. – furas Jul 03 '19 at 16:13
  • [Python nonblocking console input](https://stackoverflow.com/questions/2408560/python-nonblocking-console-input) – furas Jul 03 '19 at 16:15

1 Answers1

0

There is no standard way to do this in Python, but there are several potential solutions. One of the cleaner ways is to register an alarm before your call to raw_input. Look at the answer to this question here.

JimPri
  • 1,278
  • 8
  • 17