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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### 多線程-threading python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用 ##### 1. 使用threading模塊 單線程執行 ~~~ #coding=utf-8 import time def saySorry(): print("親愛的,我錯了,我能吃飯了嗎?") time.sleep(1) if __name__ == "__main__": for i in range(5): saySorry() ~~~ 運行結果: ![](https://box.kancloud.cn/29eb08e2e2d85d95a7273bc7703f6fad_434x84.gif) 多線程執行 ~~~ #coding=utf-8 import threading import time def saySorry(): print("親愛的,我錯了,我能吃飯了嗎?") time.sleep(1) if __name__ == "__main__": for i in range(5): t = threading.Thread(target=saySorry) t.start() #啟動線程,即讓線程開始執行 ~~~ 運行結果: ![](https://box.kancloud.cn/20cbdf29022fcbf99f9d4764c557b115_433x80.gif) >[warning] 說明 * 可以明顯看出使用了多線程并發的操作,花費時間要短很多 * 創建好的線程,需要調用start()方法來啟動 ##### 2. 主線程會等待所有的子線程結束后才結束 ~~~ #coding=utf-8 import threading from time import sleep,ctime def sing(): for i in range(3): print("正在唱歌...%d"%i) sleep(1) def dance(): for i in range(3): print("正在跳舞...%d"%i) sleep(1) if __name__ == '__main__': print('---開始---:%s'%ctime()) t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() #sleep(5) # 屏蔽此行代碼,試試看,程序是否會立馬結束? print('---結束---:%s'%ctime()) ~~~ 運行結果: ![](https://box.kancloud.cn/97573bd65d8809d46aed13bd83e5a0f8_715x241.gif) ##### 3. 查看線程數量 ~~~ #coding=utf-8 import threading from time import sleep,ctime def sing(): for i in range(3): print("正在唱歌...%d"%i) sleep(1) def dance(): for i in range(3): print("正在跳舞...%d"%i) sleep(1) if __name__ == '__main__': print('---開始---:%s'%ctime()) t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() while True: length = len(threading.enumerate()) print('當前運行的線程數為:%d'%length) if length<=1: break sleep(0.5) ~~~ 運行結果: ![](https://box.kancloud.cn/b60c27033281a492e3085472fd24bffd_715x347.gif)
                  <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>

                              哎呀哎呀视频在线观看