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.

4
  • stackoverflow.com/questions/30929661/… Commented Jul 3, 2019 at 15:28
  • Maybe it's just how Windows works the but "sleep" never ends Commented Jul 3, 2019 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. Commented Jul 3, 2019 at 16:13
  • Python nonblocking console input Commented Jul 3, 2019 at 16:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.