跳过输入函数超时 [英] Skip the input function with timeout
本文介绍了跳过输入函数超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用 Python 3.7 编写任何程序.
I'm making any program in Python 3.7.
我想在特定时间后跳过 input
功能.
I want to skip input
function after a specific time.
我的代码结构类似于下面的粗略代码.
My code has the structure like the following rough code.
def functionA():
...(skip)...
def functionB():
...(skip)...
#TIMEOUT = 0.5
while True:
TXT = None
TXT = input("Enter: ")
if TXT == None:
functionA()
elif 'NAME' in TXT:
functionB()
elif TXT == 'EXIT':
break
else:
pass
我想在 TIMEOUT
时间后跳过 TXT = input("Enter: ")
行,0.5 秒.我怎样才能按照我想要的方式制作这个流程的代码?
I wanna skip the line TXT = input("Enter: ")
after TIMEOUT
time, 0.5 sec. How can I make the code of this flow the way I want?
推荐答案
您可以使用 inputimeout
模块
您可以通过运行 cmd 并输入此命令来安装模块
You can install the module by running cmd and typing this command
pip install inputimeout
你可以这样使用
from inputimeout import inputimeout, TimeoutOccurred
try:
var = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
var = ''
使用步骤
- 导入文件中的模块
- 启动 try 方法
- 创建一个变量,而不是输入使用
inputimeout
函数并输入值as prompt=
和timeout=
- 在
except TimeoutOccurred:
输入 var 的值,如果发生超时
- Import the module in file
- start the try method
- make a variable and instead of input use
inputimeout
function and enter values as prompt=
andtimeout=
- In
except TimeoutOccurred:
enter the value of the var if timeout is occured
这篇关于跳过输入函数超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文