<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `sleep()` > 原文: [https://www.programiz.com/python-programming/time/sleep](https://www.programiz.com/python-programming/time/sleep) #### `sleep()`函數在給定的秒數內掛起(等待)當前線程的執行。 Python 有一個名為[`time`](/python-programming/time "Python time Module")的模塊,該模塊提供了一些有用的函數來處理與時間有關的任務。 其中最流行的函數之一是`sleep()`。 `sleep()`函數在給定的秒數內暫停當前線程的執行。 * * * ## 示例 1:Python `sleep()` ```py import time print("Printed immediately.") time.sleep(2.4) print("Printed after 2.4 seconds.") ``` 該程序的工作原理如下: * 打印`"Printed immediately"` * 暫停(延遲)執行 2.4 秒。 * 打印`"Printed after 2.4 seconds"`。 從上面的示例中可以看到,`sleep()`以浮點數作為參數。 **在 Python 3.5** 之前,實際的暫停時間可能小于為`time()`函數指定的參數。 **從 Python 3.5** 開始,暫停時間將至少為指定的秒數。 * * * ## 示例 2:Python 創建數字時鐘 ```py import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1) ``` 在上面的程序中,我們在[無限循環](/python-programming/while-loop)內計算并打印了當前本地時間。 然后,程序等待 1 秒鐘。 同樣,將計算并打印當前本地時間。 這個過程繼續進行。 當您運行程序時,輸出將類似于: ```py 02:10:50 PM 02:10:51 PM 02:10:52 PM 02:10:53 PM 02:10:54 PM ... .. ... ``` 這是上述程序的稍作修改的更好的版本。 ```py import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result, end="", flush=True) print("\r", end="", flush=True) time.sleep(1) ``` 要了解更多信息,請訪問 Python shell 中的[數字時鐘](https://stackoverflow.com/questions/37515587/run-a-basic-digital-clock-in-the-python-shell)。 * * * ## Python 中的多線程 在討論多線程程序中的`sleep()`之前,讓我們討論一下進程和線程。 計算機程序是指令的集合。 流程就是這些指令的執行。 線程是進程的子集。 一個進程可以具有一個或多個線程。 * * * ### 示例 3:Python 多線程 本文上面的所有程序都是單線程程序。 這是一個多線程 Python 程序的示例。 ```py import threading def print_hello_three_times(): for i in range(3): print("Hello") def print_hi_three_times(): for i in range(3): print("Hi") t1 = threading.Thread(target=print_hello_three_times) t2 = threading.Thread(target=print_hi_three_times) t1.start() t2.start() ``` 當您運行程序時,輸出將類似于: ```py Hello Hello Hi Hello Hi Hi ``` 上面的程序有兩個線程`t1`和`t2`。 這些線程使用`t1.start()`和`t2.start()`語句運行。 請注意,`t1`和`t2`同時運行,您可能會獲得不同的輸出。 訪問此頁面以了解有關 [Python](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) 中的多線程的更多信息。 * * * ## 多線程程序中的`time.sleep()` `sleep()`函數將當前線程的執行暫停給定的秒數。 對于單線程程序,`sleep()`暫停線程和進程的執行。 但是,該函數在多線程程序中掛起線程而不是整個進程。 * * * ### 示例 4:多線程程序中的`sleep()` ```py import threading import time def print_hello(): for i in range(4): time.sleep(0.5) print("Hello") def print_hi(): for i in range(4): time.sleep(0.7) print("Hi") t1 = threading.Thread(target=print_hello) t2 = threading.Thread(target=print_hi) t1.start() t2.start() ``` 上面的程序有兩個線程。 我們已經使用`time.sleep(0.5)`和`time.sleep(0.75)`將這兩個線程的執行分別暫停 0.5 秒和 0.7 秒。 **推薦閱讀**: [Python `time.sleep()`](https://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process)使線程休眠
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看