python 進(jìn)程池的兩種不同實現(xiàn)方法示例
更新時間:2023年05月31日 10:35:01 作者:ponponon
這篇文章主要為大家介紹了python 進(jìn)程池的兩種不同實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
方式一:使用 multiprocessing 庫
from loguru import logger
import multiprocessing
def start_request(message: str) -> int:
try:
logger.debug(message)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=2)
for message in ['haha', 'hehe']:
pool.apply_async(start_request, (message,))
pool.close()
pool.join()方式二:使用 concurrent.futures 的 ProcessPoolExecutor
from loguru import logger
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
def start_request(message: str) -> int:
try:
logger.debug(message)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
pool = ProcessPoolExecutor(
max_workers=2
)
for message in ['haha', 'hehe']:
pool.submit(start_request, message)
pool.shutdown(wait=True)以上就是python 進(jìn)程池的兩種不同實現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于python 進(jìn)程兩種實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用PyTorch實現(xiàn)手寫數(shù)字識別功能
在人工智能的世界里,計算機視覺是最具魅力的領(lǐng)域之一,通過PyTorch這一強大的深度學(xué)習(xí)框架,我們將在經(jīng)典的MNIST數(shù)據(jù)集上,見證一個神經(jīng)網(wǎng)絡(luò)從零開始學(xué)會識別數(shù)字的全過程,本文給大家介紹了如何使用PyTorch實現(xiàn)手寫數(shù)字識別,需要的朋友可以參考下2025-03-03
Python列表list常用內(nèi)建函數(shù)實例小結(jié)
這篇文章主要介紹了Python列表list常用內(nèi)建函數(shù),結(jié)合實例形式總結(jié)分析了Python列表list常見內(nèi)建函數(shù)的功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-10-10
詳細(xì)整理python 字符串(str)與列表(list)以及數(shù)組(array)之間的轉(zhuǎn)換方法
這篇文章主要介紹了詳細(xì)整理python 字符串(str)與列表(list)以及數(shù)組(array)之間的轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

