How to set time limit on raw_input
The signal.alarm function, on which @jer's recommended solution is based, is unfortunately Unix-only. If you need a cross-platform or Windows-specific solution, you can base it on threading.Timer instead, using thread.interrupt_main to send a KeyboardInterrupt
to the main thread from the timer thread. I.e.:
import thread
import threading
def raw_input_with_timeout(prompt, timeout=30.0):
print(prompt, end=' ')
timer = threading.Timer(timeout, thread.interrupt_main)
astring = None
try:
timer.start()
astring = input(prompt)
except KeyboardInterrupt:
pass
timer.cancel()
return astring
this will return None whether the 30 seconds time out or the user explicitly decides to hit control-C to give up on inputting anything, but it seems OK to treat the two cases in the same way (if you need to distinguish, you could use for the timer a function of your own that, before interrupting the main thread, records somewhere the fact that a timeout has happened, and in your handler for KeyboardInterrupt
access that "somewhere" to discriminate which of the two cases occurred).
Edit: I could have sworn this was working but I must have been wrong -- the code above omits the obviously-needed timer.start()
, and even with it I can't make it work any more. select.select
would be the obvious other thing to try but it won't work on a "normal file" (including stdin) in Windows -- in Unix it works on all files, in Windows, only on sockets.
So I don't know how to do a cross-platform "raw input with timeout". A windows-specific one can be constructed with a tight loop polling msvcrt.kbhit, performing a msvcrt.getche
(and checking if it's a return to indicate the output's done, in which case it breaks out of the loop, otherwise accumulates and keeps waiting) and checking the time to time out if needed. I cannot test because I have no Windows machine (they're all Macs and Linux ones), but here the untested code I would suggest:
import msvcrt
import time
def raw_input_with_timeout(prompt, timeout=30.0):
print(prompt, end=' ')
finishat = time.time() + timeout
result = []
while True:
if msvcrt.kbhit():
result.append(msvcrt.getche())
if result[-1] == '\r': # or \n, whatever Win returns;-)
return ''.join(result)
time.sleep(0.1) # just to yield to other processes/threads
else:
if time.time() > finishat:
return None
The OP in a comment says he does not want to return None
upon timeout, but what's the alternative? Raising an exception? Returning a different default value? Whatever alternative he wants he can clearly put it in place of my return None
;-).
If you don't want to time out just because the user is typing slowly (as opposed to, not typing at all!-), you could recompute finishat after every successful character input.
Keyboard input with timeout?
The example you have linked to is wrong and the exception is actually occuring when calling alarm handler instead of when read blocks. Better try this:
import signal
TIMEOUT = 5 # number of seconds your want for timeout
def interrupted(signum, frame):
"called when read times out"
print 'interrupted!'
signal.signal(signal.SIGALRM, interrupted)
def input():
try:
print 'You have 5 seconds to type in your stuff...'
foo = raw_input()
return foo
except:
# timeout
return
# set alarm
signal.alarm(TIMEOUT)
s = input()
# disable the alarm after success
signal.alarm(0)
print 'You typed', s
make python raw_input() or input() not waiting
One potential option:
import select, sys
r, w, x = select.select([sys.stdin], [], [], 3)
This will block for ~3 seconds - any data present will be in r
after.
Timed input with Python 3 and Windows 7
Finally I modified @Darkonaut answer (Thank you!) to match my first situation and I added a "simulated keyboard" with the library pynput
to automatically press "Enter".
Note that this works in Terminal (Python 3.6.8 and Windows 7 SP1) but DOESN'T WORK IF STARTED WITH IDLE.
from threading import Thread, enumerate, Event
from queue import Queue, Empty
import time
from pynput.keyboard import Key, Controller
SENTINEL = None
class PromptManager(Thread):
def __init__(self, timeout):
super().__init__()
self.timeout = timeout
self._in_queue = Queue()
self._out_queue = Queue()
self.prompter = Thread(target=self._prompter, daemon=True)
self._prompter_exit = Event()
def run(self):
"""Run worker-thread. Start prompt-thread, fetch passed
input from in_queue and forward it to `._poll()` in MainThread.
If timeout occurs before user-input, enqueue SENTINEL to
unblock `.get()` in `._poll()`.
"""
self.prompter.start()
try:
txt = self._in_queue.get(timeout=self.timeout)
except Empty:
self._out_queue.put(SENTINEL)
print(f"\n[{time.ctime()}] Please press Enter to continue.")
# without usage of _prompter_exit() and Enter, the
# prompt-thread would stay alive until the whole program ends
keyboard = Controller()
keyboard.press(Key.enter)
keyboard.release(Key.enter)
self._prompter_exit.wait()
else:
self._out_queue.put(txt)
def start(self):
"""Start manager-thread."""
super().start()
return self._poll()
def _prompter(self):
"""Prompting target function for execution in prompter-thread."""
self._in_queue.put(input(f"[{time.ctime()}] >$ "))
self._prompter_exit.set()
def _poll(self):
"""Get forwarded inputs from the manager-thread executing `run()`
and process them in the parent-thread.
"""
msg = self._out_queue.get()
self.join()
return msg
def input_with_timeout(default, timeout):
print ("Hello, you can type and press enter to change 'ans' variable value or wait "+str(timeout)+" seconds and the program will continue")
pm = PromptManager(timeout)
ans= pm.start()
if isinstance(ans, str):
print("ok")
return ans
else:
return default
s="mustnotchange"
s=input_with_timeout(s,5)
if s=="mustnotchange":
print("Success, if you didn't just cheat by writing mustnotchange")
else:
print("you answered : "+s+" variable value has changed")
time.sleep(5)
Related Topics
"Importerror: No Module Named Site" on Windows
How to Create Multiline Comments in Python
Python Process Pool Non-Daemonic
How to Dynamically Change Base Class of Instances at Runtime
Pandas 'Count(Distinct)' Equivalent
Python: Defining My Own Operators
String Concatenation Without '+' Operator
How to Remove Specific Elements in a Numpy Array
Why Does Python Code Use Len() Function Instead of a Length Method
How to Quantify Difference Between Two Images
Running Selenium with Headless Chrome Webdriver
Df.Append() Is Not Appending to the Dataframe
Convert Unix Time to Readable Date in Pandas Dataframe
Draw a Transparent Rectangles and Polygons in Pygame