Python多線(xiàn)程多進(jìn)程實(shí)例對(duì)比解析
多線(xiàn)程適合于多io操作
多進(jìn)程適合于耗cpu(計(jì)算)的操作
# 多進(jìn)程編程 # 耗cpu的操作,用多進(jìn)程編程, 對(duì)于io操作來(lái)說(shuō),使用多線(xiàn)程編程 import time from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ProcessPoolExecutor def fib(n): if n <= 2: return 1 return fib(n - 2) + fib(n - 1) if __name__ == '__main__': # 1. 對(duì)于耗cpu操作,多進(jìn)程優(yōu)于多線(xiàn)程 # with ThreadPoolExecutor(3) as executor: # all_task = [executor.submit(fib, num) for num in range(25, 35)] # start_time = time.time() # for future in as_completed(all_task): # data = future.result() # print(data) # print("last time :{}".format(time.time() - start_time)) # 3.905290126800537 # 多進(jìn)程 ,在window環(huán)境 下必須放在main方法中執(zhí)行,否則拋異常 with ProcessPoolExecutor(3) as executor: all_task = [executor.submit(fib, num) for num in range(25, 35)] start_time = time.time() for future in as_completed(all_task): data = future.result() print(data) print("last time :{}".format(time.time() - start_time)) # 2.6130592823028564
可以看到在耗cpu的應(yīng)用中,多進(jìn)程明顯優(yōu)于多線(xiàn)程 2.6130592823028564 < 3.905290126800537
下面模擬一個(gè)io操作
# 多進(jìn)程編程 # 耗cpu的操作,用多進(jìn)程編程, 對(duì)于io操作來(lái)說(shuō),使用多線(xiàn)程編程 import time from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ProcessPoolExecutor def io_operation(n): time.sleep(2) return n if __name__ == '__main__': # 1. 對(duì)于耗cpu操作,多進(jìn)程優(yōu)于多線(xiàn)程 # with ThreadPoolExecutor(3) as executor: # all_task = [executor.submit(io_operation, num) for num in range(25, 35)] # start_time = time.time() # for future in as_completed(all_task): # data = future.result() # print(data) # print("last time :{}".format(time.time() - start_time)) # 8.00358772277832 # 多進(jìn)程 ,在window環(huán)境 下必須放在main方法中執(zhí)行,否則拋異常 with ProcessPoolExecutor(3) as executor: all_task = [executor.submit(io_operation, num) for num in range(25, 35)] start_time = time.time() for future in as_completed(all_task): data = future.result() print(data) print("last time :{}".format(time.time() - start_time)) # 8.12435245513916
可以看到 8.00358772277832 < 8.12435245513916, 即是多線(xiàn)程比多進(jìn)程更牛逼!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python多進(jìn)程與多線(xiàn)程的使用場(chǎng)景詳解
- Python之多進(jìn)程與多線(xiàn)程的使用
- python多線(xiàn)程和多進(jìn)程關(guān)系詳解
- Python 多進(jìn)程、多線(xiàn)程效率對(duì)比
- Python全局鎖中如何合理運(yùn)用多線(xiàn)程(多進(jìn)程)
- python線(xiàn)程安全及多進(jìn)程多線(xiàn)程實(shí)現(xiàn)方法詳解
- Python實(shí)現(xiàn)多線(xiàn)程/多進(jìn)程的TCP服務(wù)器
- python多線(xiàn)程與多進(jìn)程及其區(qū)別詳解
- 處理python中多線(xiàn)程與多進(jìn)程中的數(shù)據(jù)共享問(wèn)題
- Python2.7實(shí)現(xiàn)多進(jìn)程下開(kāi)發(fā)多線(xiàn)程示例
- python 多進(jìn)程和多線(xiàn)程使用詳解
相關(guān)文章
解決import tensorflow導(dǎo)致jupyter內(nèi)核死亡的問(wèn)題
這篇文章主要介紹了解決import tensorflow導(dǎo)致jupyter內(nèi)核死亡的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè)
這篇文章主要為大家詳細(xì)介紹了Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02python使用datetime模塊計(jì)算各種時(shí)間間隔的方法
這篇文章主要介紹了python使用datetime模塊計(jì)算各種時(shí)間間隔的方法,實(shí)例分析了Python使用datetime模塊進(jìn)行各種常用的時(shí)間操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03在Python中操作列表之List.append()方法的使用
這篇文章主要介紹了在Python中操作列表之List.append()方法的使用,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05在langchain中對(duì)大模型的輸出進(jìn)行格式化實(shí)現(xiàn)
這篇文章主要為大家介紹了在langchain中對(duì)大模型的輸出進(jìn)行格式化實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Python淘寶或京東等秒殺搶購(gòu)腳本實(shí)現(xiàn)(秒殺腳本)
本篇文章主要介紹了Python 通過(guò)selenium實(shí)現(xiàn)毫秒級(jí)自動(dòng)搶購(gòu)的示例代碼,通過(guò)掃碼登錄即可自動(dòng)完成一系列操作,搶購(gòu)時(shí)間精確至毫秒,可搶加購(gòu)物車(chē)等待時(shí)間結(jié)算的,也可以搶聚劃算、火車(chē)票等的商品,感興趣的朋友跟隨小編一起看看吧2022-10-10typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié)
當(dāng)在 Python 函數(shù)中聲明一個(gè) dictionary 作為參數(shù)時(shí),我們一般會(huì)把 key 和 value 的數(shù)據(jù)類(lèi)型聲明為全局變量,而不是局部變量。,這篇文章主要介紹了typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié),需要的朋友可以參考下2023-06-06