我如何让我的Python程序睡眠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
其他回答
使用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)
请注意,如果你的睡眠时间恰好是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
你也可以这样使用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)
推荐文章
- python中的assertEquals和assertEqual
- 如何保持Python打印不添加换行符或空格?
- 为什么Python的无穷散列中有π的数字?
- Python 3.7数据类中的类继承
- 如何在PyTorch中初始化权重?
- 计数唯一的值在一列熊猫数据框架像在Qlik?
- 使用Pandas将列转换为行
- 从matplotlib中的颜色映射中获取单个颜色
- 将Pandas或Numpy Nan替换为None以用于MysqlDB
- 使用pandas对同一列进行多个聚合
- 使用Python解析HTML
- django MultiValueDictKeyError错误,我如何处理它
- 如何在for循环期间修改列表条目?
- 我如何在Django中创建一个鼻涕虫?
- 没有名为'django.core.urlresolvers'的模块