python進程池和線程池的區(qū)別
線程池和多線程的區(qū)別:
線程池可以減少線程創(chuàng)建和銷毀的開銷,并控制并發(fā)數(shù)量,從而提高系統(tǒng)的性能。多線程可以并發(fā)執(zhí)行程序中的不同任務(wù),提高程序的效率和系統(tǒng)的吞吐量。
如果需要同時處理多個任務(wù)并且這些任務(wù)之間沒有依賴關(guān)系,那么可以選擇多線程;如果需要限制同時執(zhí)行的任務(wù)數(shù)量或者需要復(fù)用線程來避免頻繁創(chuàng)建和銷毀線程的開銷或者當(dāng)需要執(zhí)行的任務(wù)數(shù)量很大但是每個任務(wù)所需的時間較短并且執(zhí)行時間相對穩(wěn)定,那么可以選擇線程池。
進程池和多進程的區(qū)別:
如果你需要同時執(zhí)行多個不同的任務(wù),使用多進程;如果你需要并行處理多個任務(wù),并且每個任務(wù)都是獨立的,那么多進程會是一個不錯的選擇。每個進程都有自己的內(nèi)存空間和獨立的 CPU 時間,可以避免多個任務(wù)之間的相互干擾。
如果你需要限制并發(fā)執(zhí)行任務(wù)的數(shù)量,使用進程池。如果你的任務(wù)數(shù)比較大,而且每個任務(wù)執(zhí)行時間較短,那么進程池可能更適合。進程池會預(yù)先創(chuàng)建一定數(shù)量的進程,任務(wù)到來時就將其分配給空閑的進程執(zhí)行,避免反復(fù)創(chuàng)建和銷毀進程的開銷。
線程池的使用:
from concurrent.futures import ThreadPoolExecutor, as_completed, wait, ALL_COMPLETED def test(a): print(f'一個假的進程任務(wù){(diào)a}') return a def add_thread(): threads_list = [i for i in range(10)] th_executor = ThreadPoolExecutor(max_workers=10) for i in threads_list: # 線程池執(zhí)行任務(wù)方法1 th_executor.submit(test, i) # 線程池執(zhí)行任務(wù)方法2 th_executor.map(test, threads_list) # 線程池獲取結(jié)果方法1:使用as_completed方法,然后用其result屬性輸出結(jié)果 all_thread = [th_executor.submit(test, i) for i in threads_list] results1 = [j.result() for j in as_completed(all_thread)] print('result1結(jié)果:', results1) # result1結(jié)果: [3, 0, 2, 4, 1, 5, 6, 8, 9, 7] # 線程池獲取結(jié)果方法2 results2 = [k for k in th_executor.map(test, threads_list)] print('results2結(jié)果:', results2) # results2結(jié)果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 也可以使用等待函數(shù),fs:任務(wù)序列;timeout任務(wù)最大等待時間,在規(guī)定時間內(nèi),任務(wù)未執(zhí)行完成也會停止; # return_when:wait返回結(jié)果的條件,默認(rèn)為ALL_COMPLETED,全部執(zhí)行完成再返回 # 譬如,此處以下的代碼是展示wait的用法 # task_list = th_executor.map(test, threads_list) # 等待任務(wù)完成 # wait(task_list) # 任務(wù)完成之后再進行結(jié)果的獲取 # task_result = [k for k in task_list] if __name__ == '__main__': add_thread()
由result1結(jié)果和result2結(jié)果可知,使用submit方法執(zhí)行任務(wù)的順序是不固定的,使用map方法執(zhí)行任務(wù)的順序是固定的。
進程池的用法:和線程池是一樣的;進程池用的是with方法去展示的,也可以用線程池的那種方法是使用,當(dāng)然線程池的也可以用with方法去使用。
from concurrent.futures import ProcessPoolExecutor, as_completed, wait, ALL_COMPLETED def test(a): print(f'一個假的線程任務(wù){(diào)a}') return a def add_process(): with ProcessPoolExecutor(max_workers=8) as pool: process_list = [i for i in range(10)] for i in process_list: # 進程池執(zhí)行任務(wù)方法1 pool.submit(test, i) # 進程池執(zhí)行任務(wù)方法2 pool.map(test, process_list) # 進程池獲取結(jié)果方法1:使用as_completed方法,然后用其result屬性輸出結(jié)果 all_process = [pool.submit(test, i) for i in process_list] results1 = [j.result() for j in as_completed(all_process)] print('result1結(jié)果:', results1) # results1結(jié)果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 進程池獲取結(jié)果方法2 results2 = [k for k in pool.map(test, process_list)] print('results2結(jié)果:', results2) # results2結(jié)果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 也可以使用等待函數(shù),fs:任務(wù)序列;timeout任務(wù)最大等待時間,在規(guī)定時間內(nèi),任務(wù)未執(zhí)行完成也會停止; # return_when:wait返回結(jié)果的條件,默認(rèn)為ALL_COMPLETED,全部執(zhí)行完成再返回 # 譬如,此處是展示wait的用法 # task_list = pool.map(test, process_list) # # 等待任務(wù)完成 # wait(task_list) # # 任務(wù)完成之后再進行結(jié)果的獲取 # task_result = [k for k in task_list] if __name__ == '__main__': add_process()
由result1結(jié)果和result2結(jié)果可知,使用submit方法執(zhí)行任務(wù)的順序和使用map方法執(zhí)行任務(wù)的順序都是固定的。
到此這篇關(guān)于python進程池和線程池的區(qū)別的文章就介紹到這了,更多相關(guān)python進程池和線程池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中__call__內(nèi)置函數(shù)用法實例
這篇文章主要介紹了python中__call__內(nèi)置函數(shù)用法,實例分析了python中__call__內(nèi)置函數(shù)的原理與使用技巧,需要的朋友可以參考下2015-06-06使用?Flask、Celery?和?Python?實現(xiàn)每月定時任務(wù)的步驟
下面給大家分享使用?Flask、Celery?和?Python?實現(xiàn)每月定時任務(wù)的步驟,本文分步驟結(jié)合腳本給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-08-08基于logstash實現(xiàn)日志文件同步elasticsearch
這篇文章主要介紹了基于logstash實現(xiàn)日志文件同步elasticsearch,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08python實現(xiàn)數(shù)通設(shè)備端口監(jiān)控示例
這篇文章主要介紹了python實現(xiàn)數(shù)通設(shè)備端口監(jiān)控示例,需要的朋友可以參考下2014-04-04