Python技巧之四種多線程應用分享
在Python中,多線程是實現(xiàn)并發(fā)的一種方式。多線程可以讓程序在同一時間內(nèi)進行多個任務,從而提高程序的效率和執(zhí)行速度。
本文將介紹Python中多線程的所有方式,包括使用threading模塊、使用concurrent.futures模塊、使用multiprocessing模塊以及使用asyncio模塊。
1.使用threading模塊
Python中的threading模塊提供了多線程編程的基本支持。使用該模塊可以創(chuàng)建和管理線程,從而實現(xiàn)并發(fā)執(zhí)行。下面是使用threading模塊實現(xiàn)多線程的示例代碼:
import?threading def?worker(): ????print('Worker?thread?started') ????#?do?some?work?here ????print('Worker?thread?finished') if?__name__?==?'__main__': ????print('Main?thread?started') ????#?create?a?new?thread ????t?=?threading.Thread(target=worker) ????#?start?the?new?thread ????t.start() ????print('Main?thread?finished')
在上面的代碼中,我們首先定義了一個worker函數(shù),該函數(shù)會在一個新的線程中執(zhí)行。
然后,在主線程中創(chuàng)建了一個新的線程t,并將worker函數(shù)作為該線程的目標。
最后,通過調(diào)用start方法來啟動新線程。運行上面的代碼,輸出結(jié)果如下:
Main thread started
Worker thread started
Main thread finished
Worker thread finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主線程中的代碼,然后創(chuàng)建了一個新的線程,并在新線程中執(zhí)行worker函數(shù)。
主線程和新線程是并行執(zhí)行的,因此程序的執(zhí)行速度得到了提高。
2.使用concurrent.futures模塊
concurrent.futures模塊是Python 3中的新模塊,它提供了線程池和進程池的實現(xiàn)。使用該模塊可以更方便地實現(xiàn)并行執(zhí)行。
下面是使用concurrent.futures模塊實現(xiàn)多線程的示例代碼:
import?concurrent.futures def?worker(): ????print('Worker?thread?started') ????#?do?some?work?here ????print('Worker?thread?finished') if?__name__?==?'__main__': ????print('Main?thread?started') ????#?create?a?thread?pool ????with?concurrent.futures.ThreadPoolExecutor(max_workers=2)?as?executor: ????????#?submit?worker?function?to?the?pool ????????future?=?executor.submit(worker) ????????print('Main?thread?finished')
在上面的代碼中,我們首先定義了一個worker函數(shù),該函數(shù)會在一個新的線程中執(zhí)行。
然后,在主線程中創(chuàng)建了一個線程池executor,并設置最大線程數(shù)為2。接著,通過調(diào)用submit方法將worker函數(shù)提交給線程池。
最后,我們輸出了一條信息,表示主線程已經(jīng)執(zhí)行完畢。運行上面的代碼,輸出結(jié)果如下:
Main thread started
Main thread finished
Worker thread started
Worker thread finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主線程中的代碼,然后通過線程池執(zhí)行了worker函數(shù)。線程池會自動管理線程的創(chuàng)建和銷毀,從而使程序更加高效。
3.使用multiprocessing模塊
Python中的multiprocessing模塊提供了多進程編程的支持。使用該模塊可以在不同的進程中執(zhí)行任務,從而實現(xiàn)并發(fā)執(zhí)行。
下面是使用multiprocessing模塊實現(xiàn)多線程的示例代碼:
import?multiprocessing def?worker(): ????print('Worker?process?started') ????#?do?some?work?here ????print('Worker?process?finished') if?__name__?==?'__main__': ????print('Main?process?started') ????#?create?a?new?process ????p?=?multiprocessing.Process(target=worker) ????#?start?the?new?process ????p.start() ????print('Main?process?finished')
在上面的代碼中,我們首先定義了一個worker函數(shù),該函數(shù)會在一個新的進程中執(zhí)行。然后,在主進程中創(chuàng)建了一個新的進程p,并將worker函數(shù)作為該進程的目標。
最后,通過調(diào)用start方法來啟動新進程。運行上面的代碼,輸出結(jié)果如下:
Main process started
Main process finished
Worker process started
Worker process finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主進程中的代碼,然后創(chuàng)建了一個新的進程,并在新進程中執(zhí)行worker函數(shù)。
主進程和新進程是并行執(zhí)行的,因此程序的執(zhí)行速度得到了提高。
4.使用asyncio模塊
Python中的asyncio模塊提供了異步編程的支持。使用該模塊可以實現(xiàn)協(xié)程,從而在單線程中實現(xiàn)并發(fā)執(zhí)行。
下面是使用asyncio模塊實現(xiàn)多線程的示例代碼:
import?asyncio async?def?worker(): ????print('Worker?task?started') ????#?do?some?work?here ????print('Worker?task?finished') if?__name__?==?'__main__': ????print('Main?task?started') ????#?create?a?new?event?loop ????loop?=?asyncio.get_event_loop() ????#?run?the?worker?coroutine ????loop.run_until_complete(worker()) ????#?close?the?event?loop ????loop.close() ????print('Main?task?finished')
在上面的代碼中,我們首先定義了一個異步函數(shù)worker,該函數(shù)會在一個協(xié)程中執(zhí)行。
然后,在主任務中創(chuàng)建了一個新的事件循環(huán)loop,并通過調(diào)用run_until_complete方法來運行worker協(xié)程。
最后,我們關閉了事件循環(huán)。運行上面的代碼,輸出結(jié)果如下:
Main task started
Worker task started
Worker task finished
Main task finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主任務中的代碼,然后通過事件循環(huán)執(zhí)行了worker協(xié)程。
協(xié)程是在單線程中執(zhí)行的,因此程序的執(zhí)行速度得到了提高。
5.總結(jié)
本文介紹了Python中多線程的所有方式,包括使用threading模塊、使用concurrent.futures模塊、使用multiprocessing模塊以及使用asyncio模塊。
不同的方式適用于不同的場景,可以根據(jù)需要選擇最合適的方式。
多線程編程可以提高程序的效率和執(zhí)行速度,但需要注意線程安全和鎖的使用。
到此這篇關于Python技巧之四種多線程應用分享的文章就介紹到這了,更多相關Python多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python continue繼續(xù)循環(huán)用法總結(jié)
本篇文章給大家總結(jié)了關于Python continue繼續(xù)循環(huán)的相關知識點以及用法,有需要的朋友跟著學習下吧。2018-06-06基于SQLAlchemy實現(xiàn)操作MySQL并執(zhí)行原生sql語句
這篇文章主要介紹了基于SQLAlchemy實現(xiàn)操作MySQL并執(zhí)行原生sql語句,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06python中關于requests里的timeout()用法
這篇文章主要介紹了python中關于requests里的timeout()用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式
這篇文章主要介紹了python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式,Reqeusts支持以form表單形式發(fā)送post請求,只需要將請求的參數(shù)構(gòu)造成一個字典,然后傳給requests.post()的data參數(shù)即可,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-08-08