python 進程池的兩種不同實現(xiàn)方法示例
更新時間:2023年05月31日 10:35:01 作者:ponponon
這篇文章主要為大家介紹了python 進程池的兩種不同實現(xià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 進程池的兩種不同實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于python 進程兩種實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
教女朋友學(xué)Python3(二)簡單的輸入輸出及內(nèi)置函數(shù)查看
這篇文章主要介紹了教女朋友學(xué)Python3(二)簡單的輸入輸出及內(nèi)置函數(shù)查看,涉及Python3簡單的輸入輸出功能實現(xiàn),以及參看內(nèi)置函數(shù)的功能和用法描述的語句,具有一定參考價值,需要的朋友可了解下。2017-11-11深度學(xué)習(xí)TextRNN的tensorflow1.14實現(xiàn)示例
這篇文章主要介紹了深度學(xué)習(xí)TextRNN的tensorflow1.14實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Python中號稱神仙的六個內(nèi)置函數(shù)詳解
這篇文章主要介紹了Python中號稱神仙的六個內(nèi)置函數(shù),今天分享的這6個內(nèi)置函數(shù),在使用?Python?進行數(shù)據(jù)分析或者其他復(fù)雜的自動化任務(wù)時非常方便,需要的朋友可以參考下2022-05-05