我正在寻找一种解决方案,使用户的输入在一段时间后超时。 此代码应在 5 秒后打印“成功...”,无需用户进行任何交互:
def input_with_timeout(timeout):
print ("Hello, you can type and press enter to change 'ans' variable value or wait "+str(timeout)+" seconds and the program will continue")
ans=input() #pass input after timeout
return ans
s="mustnotchange"
s=input_with_timeout(5)
if s=="mustnotchange":
print("Success, if you didn't just cheat by writing mustnotchange")
else:
print("you answered : "+s+" variable value has changed")
我知道这个问题经常被问到,但以下主题中提供的解决方案均不适用于 WINDOWS 7 和 Python 3(Windows 7“终极”SP1 64 位和 Python 3.6.8 64 位) Python 3 Timed Input
How to set time limit on raw_input
实际上我正在使用一个“技巧”来避免这个问题:我使用 os.startfile() 启动另一个 python 脚本,但它在启动时无法对输入使用react。
如果没有任何可行的答案,那么它一定很难做到(多处理、线程、队列...),但这在很多情况下肯定会有所帮助。
谢谢。
最佳答案
下面的代码用于使用 input()
once 进行跨平台提示
暂停。超时时,它会要求用户按 Enter 键,以便提示器-
线程可以干净地关闭。
删除影响 _prompter_exit
的部分将允许在超时时恢复,而无需用户按 Enter 键,但代价是使提示器线程保持事件状态,直到整个进程退出。
仅仅像 @Mad Physicist 建议的那样加入带有超时的提示线程是行不通的,因为它不会解锁 input()
调用本身。如果不使用特定于操作系统的库,(据我所知)如果不最终提供某种形式的输入,就无法唤醒等待 input()
的线程。
该代码是我的答案的精简版本,允许在给定时间范围内进行多个输入:
Taking in multiple inputs for a fixed time in Python
from threading import Thread, enumerate, Event
from queue import Queue, Empty
import time
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
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
用于演示:
if __name__ == '__main__':
pm = PromptManager(timeout=5)
msg = pm.start()
print(f"User input: {msg}")
for i in range(3):
print(f"[{time.ctime()}] Do something else. "
f"Alive threads:{[t.name for t in enumerate()]}")
time.sleep(1)
运行时触发超时:
[Tue Nov 26 20:50:47 2019] >$
[Tue Nov 26 20:50:52 2019] Please press Enter to continue.
User input: None
[Tue Nov 26 20:50:57 2019] Do something else. Alive threads:['MainThread']
[Tue Nov 26 20:50:58 2019] Do something else. Alive threads:['MainThread']
[Tue Nov 26 20:50:59 2019] Do something else. Alive threads:['MainThread']
Process finished with exit code 0
及时运行用户输入:
[Tue Nov 26 20:51:16 2019] >$ Hello
User input: Hello
[Tue Nov 26 20:51:19 2019] Do something else. Alive threads:['MainThread']
[Tue Nov 26 20:51:20 2019] Do something else. Alive threads:['MainThread']
[Tue Nov 26 20:51:21 2019] Do something else. Alive threads:['MainThread']
Process finished with exit code 0
关于python - Python 3 和 Windows 7 的定时输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59010120/