python-3.x - 如何在python超时后跳过用户输入
问题描述
我希望能够接收这样的用户输入:
while True:
curr_time = time.time()
timeout = curr_time+30 // after 30 seconds the bottom input code should be skipped
message = input("Your message: ")
//the rest of the code
大约 30 秒后,代码应该继续执行其余的代码,然后在 while 循环中重新开始,在该循环中将再等待 30 秒,然后才会跳过输入选项并继续循环......
解决方案
这是一个可能有帮助的代码片段?
# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
from msvcrt import getch
print "press a char key (escape key to exit)"
while True:
z = …
Well you could just use a Thread object to do the counting for you and then in your main program everytime the user enters a key, you just call a function of the Thread object counting that returns it's current value.
The example below might help.
# counterTest.py, …
推荐阅读
- python - python - 如何在剧作家中为python换行
- php - 在 PHP 中提取方括号之间的特定文本
- python - 如何使用 HTTP Digest 进行身份验证?
- python - 如何读取 data.txt 文本文件,对数据进行排序,然后使用 Python Pandas 将其转换为 DataFrame?
- javascript - SCRIPT1010:在 IE11 中运行 React 应用程序时出现预期标识符错误
- google-sheets - 如何自动获取和合并“名字和姓氏”名称并添加“@domain.com”并将所有字母小写?
- python - 使用数据框的一部分滚动指标
- azure-powershell - 如何使用 powershell 为 ADF V2 管道中的每个活动获取 ActivityType 指标?
- visual-studio - 使用 Abaqus 迁移到 OneApi Fortran 后未解析的外部符号
- c# - 获取 .net 核心中注入服务的调用者信息