python - 如何制作类似于 print() 但之后等待的函数?
问题描述
我想在 python 中为基于文本的游戏创建一个函数,它打印出给定的参数并在之后等待。
def say(string):
time.sleep(1.5)
return string
解决方案
您可以使用定期轮询,我们可以设置一个条件,所以在满足条件之前它可以等待。
import time
def wait_until(somepredicate, timeout, period=0.25, *args, **kwargs):
mustend = time.time() + timeout
while time.time() < mustend:
if somepredicate(*args, **kwargs): return True
time.sleep(period)
return False
方法2:
我们可以使用事件对象等待,下面是解释相同的链接。
https://docs.python.org/3/library/threading.html#event-objects
推荐阅读
- python - 有没有办法用新格式从 weblate 重新生成 PO 文件?
- c# - Ef Core 2 在运行时将值设置为忽略的属性
- javascript - 在Angular 6中自动滚动到组件AfterViewInit,不起作用
- java - 从 Java 程序向 Linux 终端发送命令
- qr-code - 是否有检测方向的 QR 码/条形码 API?
- angular - 禁用 Angular 2 反应形式的文本输入
- python - 如何激活另一个用户的 conda 环境?
- python - 从熊猫数据框中的地址中删除分数
- jquery - Angular 6 svg在点击时将类添加到路径
- javascript - 触发似乎不在顶部的标签的鼠标悬停事件?