欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 多進程、多線程效率對比

 更新時間:2020年11月19日 10:32:14   作者:massquantity  
這篇文章主要介紹了Python 多進程、多線程的效率對比,幫助大家選擇適合的技術(shù),感興趣的朋友可以了解下

Python 界有條不成文的準則: 計算密集型任務適合多進程,IO 密集型任務適合多線程。本篇來作個比較。

通常來說多線程相對于多進程有優(yōu)勢,因為創(chuàng)建一個進程開銷比較大,然而因為在 python 中有 GIL 這把大鎖的存在,導致執(zhí)行計算密集型任務時多線程實際只能是單線程。而且由于線程之間切換的開銷導致多線程往往比實際的單線程還要慢,所以在 python 中計算密集型任務通常使用多進程,因為各個進程有各自獨立的 GIL,互不干擾。

而在 IO 密集型任務中,CPU 時常處于等待狀態(tài),操作系統(tǒng)需要頻繁與外界環(huán)境進行交互,如讀寫文件,在網(wǎng)絡(luò)間通信等。在這期間 GIL 會被釋放,因而就可以使用真正的多線程。

以上是理論,下面做一個簡單的模擬測試: 大量計算用 math.sin() + math.cos() 來代替,IO 密集型用 time.sleep() 來模擬。 在 Python 中有多種方式可以實現(xiàn)多進程和多線程,這里一并納入看看是否有效率差異:

  1. 多進程: joblib.multiprocessing, multiprocessing.Pool, multiprocessing.apply_async, concurrent.futures.ProcessPoolExecutor
  2. 多線程: joblib.threading, threading.Thread, concurrent.futures.ThreadPoolExecutor
from multiprocessing import Pool
from threading import Thread
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time, os, math
from joblib import Parallel, delayed, parallel_backend


def f_IO(a): # IO 密集型
 time.sleep(5)

def f_compute(a): # 計算密集型
 for _ in range(int(1e7)):
  math.sin(40) + math.cos(40)
 return

def normal(sub_f):
 for i in range(6):
  sub_f(i)
 return

def joblib_process(sub_f):
 with parallel_backend("multiprocessing", n_jobs=6):
  res = Parallel()(delayed(sub_f)(j) for j in range(6))
 return


def joblib_thread(sub_f):
 with parallel_backend('threading', n_jobs=6):
  res = Parallel()(delayed(sub_f)(j) for j in range(6))
 return

def mp(sub_f):
 with Pool(processes=6) as p:
  res = p.map(sub_f, list(range(6)))
 return

def asy(sub_f):
 with Pool(processes=6) as p:
  result = []
  for j in range(6):
   a = p.apply_async(sub_f, args=(j,))
   result.append(a)
  res = [j.get() for j in result]

def thread(sub_f):
 threads = []
 for j in range(6):
  t = Thread(target=sub_f, args=(j,))
  threads.append(t)
  t.start()
 for t in threads:
  t.join()

def thread_pool(sub_f):
 with ThreadPoolExecutor(max_workers=6) as executor:
  res = [executor.submit(sub_f, j) for j in range(6)]

def process_pool(sub_f):
 with ProcessPoolExecutor(max_workers=6) as executor:
  res = executor.map(sub_f, list(range(6)))

def showtime(f, sub_f, name):
 start_time = time.time()
 f(sub_f)
 print("{} time: {:.4f}s".format(name, time.time() - start_time))

def main(sub_f):
 showtime(normal, sub_f, "normal")
 print()
 print("------ 多進程 ------")
 showtime(joblib_process, sub_f, "joblib multiprocess")
 showtime(mp, sub_f, "pool")
 showtime(asy, sub_f, "async")
 showtime(process_pool, sub_f, "process_pool")
 print()
 print("----- 多線程 -----")
 showtime(joblib_thread, sub_f, "joblib thread")
 showtime(thread, sub_f, "thread")
 showtime(thread_pool, sub_f, "thread_pool")


if __name__ == "__main__":
 print("----- 計算密集型 -----")
 sub_f = f_compute
 main(sub_f)
 print()
 print("----- IO 密集型 -----")
 sub_f = f_IO
 main(sub_f)

結(jié)果:

----- 計算密集型 -----
normal time: 15.1212s

------ 多進程 ------
joblib multiprocess time: 8.2421s
pool time: 8.5439s
async time: 8.3229s
process_pool time: 8.1722s

----- 多線程 -----
joblib thread time: 21.5191s
thread time: 21.3865s
thread_pool time: 22.5104s



----- IO 密集型 -----
normal time: 30.0305s

------ 多進程 ------
joblib multiprocess time: 5.0345s
pool time: 5.0188s
async time: 5.0256s
process_pool time: 5.0263s

----- 多線程 -----
joblib thread time: 5.0142s
thread time: 5.0055s
thread_pool time: 5.0064s

上面每一方法都統(tǒng)一創(chuàng)建6個進程/線程,結(jié)果是計算密集型任務中速度:多進程 > 單進程/線程 > 多線程, IO 密集型任務速度: 多線程 > 多進程 > 單進程/線程。

以上就是Python 多進程、多線程效率比較的詳細內(nèi)容,更多關(guān)于Python 多進程、多線程的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python實現(xiàn)mysql的單引號字符串過濾方法

    python實現(xiàn)mysql的單引號字符串過濾方法

    這篇文章主要介紹了python實現(xiàn)mysql的單引號字符串過濾方法,以一個較為詳細的實例形式分析了Python針對MySQL的操作及字符串過濾的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • python讀寫ini文件示例(python讀寫文件)

    python讀寫ini文件示例(python讀寫文件)

    項目用到數(shù)據(jù)庫,多個地方使用,不能硬編碼。ython支持ini文件的讀取,就在項目中使用了ini文件,下面是示例
    2014-03-03
  • 如何更改jupyter的默認文件路徑

    如何更改jupyter的默認文件路徑

    這篇文章主要介紹了如何更改jupyter的默認文件路徑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Python pytest.main()運行測試用例

    Python pytest.main()運行測試用例

    這篇文章主要介紹了Python pytest.main()運行測試用例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • Python實現(xiàn)求取表格文件某個區(qū)域內(nèi)單元格的最大值

    Python實現(xiàn)求取表格文件某個區(qū)域內(nèi)單元格的最大值

    這篇文章主要介紹基于Python語言,基于Excel表格文件內(nèi)某一列的數(shù)據(jù),計算這一列數(shù)據(jù)在每一個指定數(shù)量的行的范圍內(nèi)(例如每一個4行的范圍內(nèi))的區(qū)間最大值的方法,需要的朋友可以參考下
    2023-08-08
  • Python使用matplotlib繪制余弦的散點圖示例

    Python使用matplotlib繪制余弦的散點圖示例

    這篇文章主要介紹了Python使用matplotlib繪制余弦的散點圖,涉及Python操作matplotlib的基本技巧與散點的設(shè)置方法,需要的朋友可以參考下
    2018-03-03
  • 基于python goto的正確用法說明

    基于python goto的正確用法說明

    這篇文章主要介紹了基于python goto的正確用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python中的裝飾器類詳解

    Python中的裝飾器類詳解

    Python?裝飾器在很多情況下是一個非常有用的工具,它們可以用于修改或增強函數(shù)或類的行為,本篇文章將深入探討如何在?Python?中使用類裝飾器
    2023-06-06
  • python和ruby,我選誰?

    python和ruby,我選誰?

    本文給大家對比了下python和Ruby的異同以及各自的優(yōu)缺點等,向大家展示了python與Ruby的資源以及學習曲線,非常適合在此兩種語言中猶豫不決的小伙伴,希望大家能夠喜歡
    2017-09-09
  • wxPython電子表格功能wx.grid實例教程

    wxPython電子表格功能wx.grid實例教程

    這篇文章主要介紹了wxPython電子表格功能wx.grid實例教程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評論