我如何让我的Python程序睡眠50毫秒?
当前回答
使用time . sleep ():
import time
time.sleep(50 / 1000)
请参阅Python文档:https://docs.python.org/library/time.html#time.sleep
其他回答
你也可以使用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 ():
import time
time.sleep(50 / 1000)
请参阅Python文档:https://docs.python.org/library/time.html#time.sleep
使用time . sleep ()
from time import sleep
sleep(0.05)
你也可以这样使用pyautogui:
import pyautogui
pyautogui._autoPause(0.05, False)
如果第一个参数不是None,那么它将暂停第一个参数的秒,在本例中为0.05秒
如果第一个参数是None,第二个参数是True,那么它将为全局暂停设置睡眠,该设置由:
pyautogui.PAUSE = int
如果你想知道原因,请参阅源代码:
def _autoPause(pause, _pause):
"""If `pause` is not `None`, then sleep for `pause` seconds.
If `_pause` is `True`, then sleep for `PAUSE` seconds (the global pause setting).
This function is called at the end of all of PyAutoGUI's mouse and keyboard functions. Normally, `_pause`
is set to `True` to add a short sleep so that the user can engage the failsafe. By default, this sleep
is as long as `PAUSE` settings. However, this can be override by setting `pause`, in which case the sleep
is as long as `pause` seconds.
"""
if pause is not None:
time.sleep(pause)
elif _pause:
assert isinstance(PAUSE, int) or isinstance(PAUSE, float)
time.sleep(PAUSE)
请注意,如果你的睡眠时间恰好是50毫秒,你就无法达到这个效果。它只是关于它。
推荐文章
- pylab和pyplot的区别是什么?
- Argparse:确定使用了哪个子解析器
- django导入错误-没有core.management模块
- 在芹菜中检索队列中的任务列表
- 使用beautifulsoup提取属性值
- 如何禁用标准错误流的日志记录?
- 用Matplotlib在Python中绘制时间
- 类中的Python装饰器
- 在Python中锁定文件
- 得到熊猫栏目的总数
- 从pandas DataFrame中删除名称包含特定字符串的列
- Mock vs MagicMock
- 如何阅读一个。xlsx文件使用熊猫库在iPython?
- 如何访问熊猫组由数据帧按键
- Pandas和NumPy+SciPy在Python中的区别是什么?