如何让我的程序休眠 50 毫秒?

发布于 2024-07-09 18:49:43 字数 31 浏览 3 评论 0原文

如何让我的 Python 程序休眠 50 毫秒?

How do I get my Python program to sleep for 50 milliseconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奢欲 2024-07-16 18:49:43

使用 time.sleep()

from time import sleep
sleep(0.05)

Use time.sleep()

from time import sleep
sleep(0.05)
ㄟ。诗瑗 2024-07-16 18:49:43

请注意,如果您依赖睡眠恰好 50 毫秒,您将无法得到这一结果。 这只是关于它。

Note that if you rely on sleep taking exactly 50 ms, you won't get that. It will just be about it.

笑梦风尘 2024-07-16 18:49:43

使用 time.sleep()

import time
time.sleep(50 / 1000)

请参阅 Python 文档: https://docs.python.org/library/time.html#time.sleep

Use time.sleep():

import time
time.sleep(50 / 1000)

See the Python documentation: https://docs.python.org/library/time.html#time.sleep

川水往事 2024-07-16 18:49:43

有一个名为“时间”的模块可以帮助您。 我知道两种方法:

  1. 睡觉

    Sleep(参考)要求程序等待,并且然后执行其余的代码。

    有两种使用睡眠的方法:

    import time # 导入整个时间模块 
      打印(“0.00秒”) 
      time.sleep(0.05) # 50 毫秒...确保你输入了时间。   如果你导入时间! 
      打印(“0.05秒”) 
      

    第二种方式不导入整个模块,只是休眠。

    from time import sleep # 只是模块时间的 sleep 函数 
      打印(“0.00秒”) 
      sleep(0.05) # 不要放时间。   这个时候,因为它会感到困惑。   你做到了 
                  # 不导入整个模块 
      打印(“0.05秒”) 
      
  2. 使用time.monotonic()使用启动后的时间。

    如果您需要运行循环,这种方法很有用。 但这一个稍微复杂一些。 time.monotonictime.time 更好,因为它不考虑闰秒,但它会计算启动时的设置数量。 (图片来源:马克·拉卡塔

    time_not_passed = True 
      from time import monotonic as time # 导入 time.monotonic 但为了简单起见将其命名为“time” 
    
      init_time = time() # 如果导入整个模块则为 time.monotonic() 
      打印(“0.00秒”) 
      while True: # 初始化循环 
          if init_time + 0.05 <= time() and time_not_passed: # 时间未传递变量很重要,因为我们希望它运行一次。   !!!   time.monotonic() 如果整个模块导入:O 
              打印(“0.05秒”) 
              未通过时间 = False 
      

There is a module called 'time' which can help you. I know two ways:

  1. 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")
    
  2. Using time since boot using time.monotonic().

    This way is useful if you need a loop to be running. But this one is slightly more complex. time.monotonic is better than time.time as it does not account for leap seconds, but it counts the amount of settings from boot. (Credit: Mark Lakata)

    time_not_passed = True
    from time import monotonic as time # Importing time.monotonic but naming it 'time' for the sake of simplicity
    
    init_time = time() # Or time.monotonic() 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.monotonic() if whole module imported :O
            print("0.05 secs")
            time_not_passed = False
    
晚雾 2024-07-16 18:49:43

您还可以使用 Timer() 函数来完成此操作。

代码:

from threading import Timer

def hello():
  print("Hello")

t = Timer(0.05, hello)
t.start()  # After 0.05 seconds, "Hello" will be printed

You can also do it by using the Timer() function.

Code:

from threading import Timer

def hello():
  print("Hello")

t = Timer(0.05, hello)
t.start()  # After 0.05 seconds, "Hello" will be printed
始终不够爱げ你 2024-07-16 18:49:43

您还可以将 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)

You can also use pyautogui as:

import pyautogui
pyautogui._autoPause(0.05, False)

If the first argument is not None, then it will pause for first argument's seconds, in this example: 0.05 seconds

If the first argument is None, and the second argument is True, then it will sleep for the global pause setting which is set with:

pyautogui.PAUSE = int

If you are wondering about the reason, see the source code:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文