【问题标题】:How do I get my program to sleep for 50 milliseconds?如何让我的 Python 程序休眠 50 毫秒?
【发布时间】:2010-09-27 12:10:23
【问题描述】:

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

【问题讨论】:

    标签: python timer sleep


    【解决方案1】:

    使用time.sleep()

    from time import sleep
    sleep(0.05)
    

    【讨论】:

    • @CsabaToth 只要你有一个符合你的操作系统规范的 Python 实现,以上是独立于操作系统的。
    • 但它实际上是如何工作的呢?例如,实际时间分辨率通常是 16.66 毫秒(1/60 秒)吗?在这种特殊情况下,睡眠时间恰好是时间分辨率的正好 3 倍。然而,四舍五入呢?如果 3 由于浮点运算实际上是 2.9999999 并且向下舍入为 2(实际睡眠时间 = 0.0333333 s = 33.33 ms)怎么办?
    【解决方案2】:

    请注意,如果您依靠恰好 50 毫秒的睡眠时间,您将无法做到这一点。它只是关于它。

    【讨论】:

    • 它可能比某些平台上的时间长 10 或 15 毫秒,因此请注意。
    • 给定系统上的延迟是否一致?
    • @user391339 根据经验,它并不一致。线程/进程优先级、CPU 平均负载、可用内存和大量其他因素使所有调用都不精确。系统越忙,不精确度就越高。
    • 根据文档,从 Python 3.5 开始,“函数 [time.sleep(secs)] 至少休眠 secs”可能会很有趣。
    【解决方案3】:

    使用time.sleep():

    import time
    time.sleep(50 / 1000)
    

    查看 Python 文档:https://docs.python.org/library/time.html#time.sleep

    【讨论】:

      【解决方案4】:

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

      1. sleep

        Sleep (reference) 要求程序等待,然后执行其余代码。

        有两种使用睡眠的方法:

        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")
        

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

        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. Unix time以来的使用时间。

        如果您需要运行循环,这种方式很有用。但是这个稍微复杂一些。

        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
        

      【讨论】:

        【解决方案5】:

        您也可以使用Timer() 函数来做到这一点。

        代码:

        from threading import Timer
        
        def hello():
          print("Hello")
        
        t = Timer(0.05, hello)
        t.start()  # After 0.05 seconds, "Hello" will be printed
        

        【讨论】:

        • 实际上是如何实现亚秒级睡眠的?定时器的时间分辨率通常为 16.66 毫秒。
        【解决方案6】:

        你也可以使用 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)
        

        【讨论】:

        • 不知何故,最好使用time.sleep 而不是这个,但是如果你希望你的程序是纯autopygui,那么这可能是一种方式。
        猜你喜欢
        • 2010-09-10
        • 2019-03-07
        • 2021-11-20
        • 2021-06-21
        • 2011-05-10
        • 2011-08-13
        • 1970-01-01
        • 1970-01-01
        • 2011-09-06
        相关资源
        最近更新 更多