<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Python多線程并發模型在處理CPU密集任務時退化為多線程串行執行

      原因

      1. 由于GIL的存在,同一時刻只有一個線程的字節碼可以被Python解釋器執行,
        因此無論CPU有多個核心,程序執行的是CPU密集任務還是I/O密集型任務,多個線程也無法并行執行。
      2. 線程被I/O阻塞的時候會釋放GIL,因此多線程并發執行I/O密集型任務的時候,并發度尚可。
      3. 多線程執行純CPU任務的時候,不會釋放GIL,因此多線程執行純CPU任務的線程并發執行時,接近退化為串行執行。

      線程池版本-代碼運行結果

      (base) sgj@sgj-laptop:~/my-ostep/chap26/cpu-busy$ python thread_pool.py
      Seconds per round: 1.0589
      Total rounds: 55
      Total real time:66.0324
      Total theory time: 1.0589 * 55 = 58.2401
      
      線程池版本
      #!/usr/bin/env python
      
      import time
      import sys
      from concurrent.futures import ThreadPoolExecutor
      from concurrent.futures import as_completed
      
      
      def spin(duration):
          i = 0
          while i < 1.18 * duration* 10**7:
              i += 1
          return duration
      
      def main():
          # CPU密集任務中, Python 的多線程并發會退化為多線程串行
          t1 = time.time()
          spin(1)
          t2 = time.time()
          one = t2 - t1
          print(f"Seconds per round: {one:.4f}")
      
          durations = list(range(1, 11))
          with ThreadPoolExecutor() as e:
              start = time.time()
              futures = [e.submit(spin, x) for x in durations]
              gen = as_completed(futures)
              for f in gen:
                  pass
              end = time.time()
              total_rounds = sum(durations)
              print(f"Total rounds: {total_rounds}")
              print(f"Total real time:{(end - start):.4f}")
              print(f"Total theory time: {one:.4f} * {total_rounds} = {(one * total_rounds):.4f}")
      
      if __name__ == '__main__':
          main()
      

      普通多線程版本-代碼運行結果

      (base) sgj@sgj-laptop:~/my-ostep/chap26$ python threads_threading.py
      Seconds per round: 1.0434
      Total real time:64.4664
      Total rounds: 55
      Total theory time: 1.0434 * 55 = 57.3856
      
      普通多線程版本
      #!/usr/bin/env python
      
      import time
      import sys
      import threading
      
      
      def spin(duration):
          i = 0
          while i < 1.18 * duration* 10**7:
              i += 1
          return duration
      
      def main():
          # CPU密集任務中, Python 的多線程并發會退化為多線程串行
          t1 = time.time()
          spin(1)
          t2 = time.time()
          one = t2 - t1
          print(f"Seconds per round: {one:.4f}")
      
          durations = list(range(1, 11))
          lst = list()
          for duration in durations:
              t = threading.Thread(target=spin, args=(duration,))
              lst.append(t)
      
          start = time.time()
          for t in lst:
              t.start()
      
          for t in lst:
              t.join()
          end = time.time()
      
          total_rounds = sum(durations)
          print(f"Total real time:{(end - start):.4f}")
          print('Total rounds:', sum(durations))
          print(f"Total theory time: {one:.4f} * {total_rounds} = {(one * total_rounds):.4f}")
      
      if __name__ == '__main__':
          main()
      

      進程池版本-代碼運行結果

      (base) sgj@sgj-laptop:~/my-ostep/chap26/cpu-busy$ python process_pool.py
      Seconds per round: 1.1104
      Total real time:22.4188
      Total rounds: 55
      Total theory time: 1.1104 * 55 = 61.0702
      
      進程池版本
      #!/usr/bin/env python
      
      import time
      import sys
      from concurrent.futures import ProcessPoolExecutor
      from concurrent.futures import as_completed
      
      
      def spin(duration):
          i = 0
          while i < 1.18 * duration* 10**7:
              i += 1
          return duration
      
      def main():
          # python的多進程模型在多核CPU上可以并行執行
          t1 = time.time()
          spin(1)
          t2 = time.time()
          one = t2 - t1
          print(f"Seconds per round: {one:.4f}")
      
          durations = list(range(1, 11))
          with ProcessPoolExecutor() as e:
              start = time.time()
              futures = [e.submit(spin, duration) for duration in durations]
              gen = as_completed(futures)
              for f in gen:
                  pass
              end = time.time()
              total_rounds = sum(durations)
              print(f"Total real time:{(end - start):.4f}")
              print(f"Total rounds: {total_rounds}")
              print(f"Total theory time: {one:.4f} * {total_rounds} = {(one * total_rounds):.4f}")
      
      if __name__ == '__main__':
          main()
      

      普通多進程版本-代碼運行結果

      Python的多進程模型不受GIL的限制,我的代碼中創建了10個進程,但我的電腦只有8個核心,所以總的運行時間為20.6785
      如果我的電腦有10個核心,可以預見總運行時間在會在15秒左右

      (base) sgj@sgj-laptop:~/my-ostep/chap26$ python process_multiprocessing.py
      Seconds per round: 1.0555
      Total real time:20.6785
      Total rounds: 55
      Total theory time: 1.0555 * 55 = 58.0537
      
      普通多進程版本
      #!/usr/bin/env python
      
      import time
      import sys
      from multiprocessing import Process
      
      def spin(duration):
          i = 0
          while i < 1.18 * duration* 10**7:
              i += 1
          return duration
      
      def main():
          # python的多進程模型在多核CPU上可以并行執行
          t1 = time.time()
          spin(1)
          t2 = time.time()
          one = t2 - t1
          print(f"Seconds per round: {one:.4f}")
      
          durations = list(range(1, 11))
          lst = list()
          for duration in durations:
              p = Process(target=spin, args=(duration,))
              lst.append(p)
      
          start = time.time()
          for p in lst:
              p.start()
      
          for p in lst:
              p.join()
          end = time.time()
      
          total_rounds = sum(durations)
          print(f"Total real time:{(end - start):.4f}")
          print('Total rounds:', sum(durations))
          print(f"Total theory time: {one:.4f} * {total_rounds} = {(one * total_rounds):.4f}")
      
      if __name__ == '__main__':
          main()
      
      posted @ 2025-08-14 23:22  Guanjie255  閱讀(9)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲男人AV天堂午夜在| 人人爽人人爽人人片av东京热 | 亚洲乱熟乱熟女一区二区| 国产精品第二页在线播放| 欧美激情视频一区二区三区免费 | 欧美无人区码suv| 精品亚洲综合一区二区三区 | 在线观看潮喷失禁大喷水无码| 人妻另类 专区 欧美 制服| av综合网男人的天堂| 国产不卡精品一区二区三区 | 人妻日韩精品中文字幕| 亚洲国产精品久久久久秋霞| 97精品人妻系列无码人妻| 麻豆国产高清精品国在线| 国精品91人妻无码一区二区三区| 强奷白丝美女在线观看| 色五开心五月五月深深爱| 下面一进一出好爽视频| 精品国产亚洲av麻豆特色| 亚洲欧美高清在线精品一区二区| 亚洲中文字幕无码av在线| 99国产精品欧美一区二区三区| 韩国V欧美V亚洲V日本V| 国产美女高潮流白浆视频| 国产精品一区二区不卡视频| 亚洲欧美成人一区二区三区| 暖暖影院日本高清...免费| 国产午夜福利视频在线| 亚洲日韩av无码一区二区三区人 | 扒开粉嫩的小缝隙喷白浆视频| 婷婷色香五月综合缴缴情香蕉| 久久精品国产高潮国产夫妻| 亚洲AV无码破坏版在线观看 | 亚洲AV无码东方伊甸园| 亚洲丰满熟女一区二区蜜桃 | 国产一区二区三区小说| 亚洲欧洲∨国产一区二区三区| 在线观看AV永久免费| 亚洲AV永久无码嘿嘿嘿嘿| 国产精品亚洲综合一区二区|