Python技巧之四種多線(xiàn)程應(yīng)用分享
在Python中,多線(xiàn)程是實(shí)現(xiàn)并發(fā)的一種方式。多線(xiàn)程可以讓程序在同一時(shí)間內(nèi)進(jìn)行多個(gè)任務(wù),從而提高程序的效率和執(zhí)行速度。
本文將介紹Python中多線(xiàn)程的所有方式,包括使用threading模塊、使用concurrent.futures模塊、使用multiprocessing模塊以及使用asyncio模塊。
1.使用threading模塊
Python中的threading模塊提供了多線(xiàn)程編程的基本支持。使用該模塊可以創(chuàng)建和管理線(xiàn)程,從而實(shí)現(xiàn)并發(fā)執(zhí)行。下面是使用threading模塊實(shí)現(xiàn)多線(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è)新的線(xiàn)程中執(zhí)行。
然后,在主線(xiàn)程中創(chuàng)建了一個(gè)新的線(xiàn)程t,并將worker函數(shù)作為該線(xiàn)程的目標(biāo)。
最后,通過(guò)調(diào)用start方法來(lái)啟動(dòng)新線(xiàn)程。運(yùn)行上面的代碼,輸出結(jié)果如下:
Main thread started
Worker thread started
Main thread finished
Worker thread finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主線(xiàn)程中的代碼,然后創(chuàng)建了一個(gè)新的線(xiàn)程,并在新線(xiàn)程中執(zhí)行worker函數(shù)。
主線(xiàn)程和新線(xiàn)程是并行執(zhí)行的,因此程序的執(zhí)行速度得到了提高。
2.使用concurrent.futures模塊
concurrent.futures模塊是Python 3中的新模塊,它提供了線(xiàn)程池和進(jìn)程池的實(shí)現(xiàn)。使用該模塊可以更方便地實(shí)現(xiàn)并行執(zhí)行。
下面是使用concurrent.futures模塊實(shí)現(xiàn)多線(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è)新的線(xiàn)程中執(zhí)行。
然后,在主線(xiàn)程中創(chuàng)建了一個(gè)線(xiàn)程池executor,并設(shè)置最大線(xiàn)程數(shù)為2。接著,通過(guò)調(diào)用submit方法將worker函數(shù)提交給線(xiàn)程池。
最后,我們輸出了一條信息,表示主線(xiàn)程已經(jīng)執(zhí)行完畢。運(yùn)行上面的代碼,輸出結(jié)果如下:
Main thread started
Main thread finished
Worker thread started
Worker thread finished
從上面的輸出結(jié)果可以看出,程序先執(zhí)行了主線(xiàn)程中的代碼,然后通過(guò)線(xiàn)程池執(zhí)行了worker函數(shù)。線(xiàn)程池會(huì)自動(dòng)管理線(xiàn)程的創(chuàng)建和銷(xiāo)毀,從而使程序更加高效。
3.使用multiprocessing模塊
Python中的multiprocessing模塊提供了多進(jìn)程編程的支持。使用該模塊可以在不同的進(jìn)程中執(zhí)行任務(wù),從而實(shí)現(xiàn)并發(fā)執(zhí)行。
下面是使用multiprocessing模塊實(shí)現(xiàn)多線(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)。
最后,通過(guò)調(diào)用start方法來(lái)啟動(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é)程,從而在單線(xiàn)程中實(shí)現(xiàn)并發(fā)執(zhí)行。
下面是使用asyncio模塊實(shí)現(xiàn)多線(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,并通過(guò)調(diào)用run_until_complete方法來(lái)運(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ù)中的代碼,然后通過(guò)事件循環(huán)執(zhí)行了worker協(xié)程。
協(xié)程是在單線(xiàn)程中執(zhí)行的,因此程序的執(zhí)行速度得到了提高。
5.總結(jié)
本文介紹了Python中多線(xiàn)程的所有方式,包括使用threading模塊、使用concurrent.futures模塊、使用multiprocessing模塊以及使用asyncio模塊。
不同的方式適用于不同的場(chǎng)景,可以根據(jù)需要選擇最合適的方式。
多線(xiàn)程編程可以提高程序的效率和執(zhí)行速度,但需要注意線(xiàn)程安全和鎖的使用。
到此這篇關(guān)于Python技巧之四種多線(xiàn)程應(yīng)用分享的文章就介紹到這了,更多相關(guān)Python多線(xiàn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)Python random模塊打亂數(shù)組順序的實(shí)例講解
今天小編就為大家分享一篇對(duì)Python random模塊打亂數(shù)組順序的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
json 轉(zhuǎn) mot17數(shù)據(jù)格式的實(shí)現(xiàn)代碼 (親測(cè)有效)
這篇文章主要介紹了json 轉(zhuǎn) mot17數(shù)據(jù)格式的實(shí)現(xiàn)代碼 (親測(cè)有效),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
python可視化數(shù)據(jù)分析pyecharts初步嘗試
這篇文章主要為大家介紹了python可視化數(shù)據(jù)分析pyecharts初步嘗試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Pandas實(shí)現(xiàn)Dataframe的重排和旋轉(zhuǎn)
使用Pandas的pivot方法可以將DF進(jìn)行旋轉(zhuǎn)變換,本文將會(huì)詳細(xì)講解pivot的秘密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
對(duì)Python通過(guò)pypyodbc訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的方法詳解
今天小編就為大家分享一篇對(duì)Python通過(guò)pypyodbc訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

