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