我如何让我的Python程序睡眠50毫秒?
当前回答
使用time . sleep ()
from time import sleep
sleep(0.05)
其他回答
使用time . sleep ():
import time
time.sleep(50 / 1000)
请参阅Python文档:https://docs.python.org/library/time.html#time.sleep
请注意,如果你的睡眠时间恰好是50毫秒,你就无法达到这个效果。它只是关于它。
有一个叫做“时间”的模块可以帮助你。我知道两种方法:
sleep Sleep (reference) asks the program to wait, and then to do the rest of the code. There are two ways to use sleep: import time # Import whole time module print("0.00 seconds") time.sleep(0.05) # 50 milliseconds... make sure you put time. if you import time! print("0.05 seconds") The second way doesn't import the whole module, but it just sleep. from time import sleep # Just the sleep function from module time print("0.00 sec") sleep(0.05) # Don't put time. this time, as it will be confused. You did # not import the whole module print("0.05 sec") Using time since Unix time. This way is useful if you need a loop to be running. But this one is slightly more complex. time_not_passed = True from time import time # You can import the whole module like last time. Just don't forget the time. before to signal it. init_time = time() # Or time.time() if whole module imported print("0.00 secs") while True: # Init loop if init_time + 0.05 <= time() and time_not_passed: # Time not passed variable is important as we want this to run once. !!! time.time() if whole module imported :O print("0.05 secs") time_not_passed = False
你也可以使用Timer()函数来实现。
代码:
from threading import Timer
def hello():
print("Hello")
t = Timer(0.05, hello)
t.start() # After 0.05 seconds, "Hello" will be printed
使用time . sleep ()
from time import sleep
sleep(0.05)
推荐文章
- 在python中,年龄从出生日期开始
- 使用pip安装SciPy
- 在Python中,我应该如何测试变量是否为None, True或False
- 如何在Python中从毫秒创建datetime ?
- 如何解窝(爆炸)在一个熊猫数据帧列,成多行
- 如何使用pip安装opencv ?
- 在pip冻结命令的输出中“pkg-resources==0.0.0”是什么
- 格式y轴为百分比
- 熊猫连接问题:列重叠但没有指定后缀
- 为什么空字典在Python中是一个危险的默认值?
- 在Python中,冒号等于(:=)是什么意思?
- Python "SyntaxError:文件中的非ascii字符'\xe2' "
- 如何从psycopg2游标获得列名列表?
- Python中dict对象的联合
- 如何有效地比较两个无序列表(不是集合)?