Python之多進(jìn)程與多線程的使用
想象在學(xué)校的一個(gè)機(jī)房,有固定數(shù)量的電腦,老師安排了一個(gè)爬蟲(chóng)任務(wù)讓大家一起完成,每個(gè)學(xué)生使用一臺(tái)電腦爬取部分?jǐn)?shù)據(jù),將數(shù)據(jù)放到一個(gè)公共數(shù)據(jù)庫(kù)。共同資源就像公共數(shù)據(jù)庫(kù),進(jìn)程就像每一個(gè)學(xué)生,每多一個(gè)學(xué)生,就多一個(gè)進(jìn)程來(lái)完成這個(gè)任務(wù),機(jī)房里的電腦數(shù)量就像CPU,所以進(jìn)程數(shù)量是CPU決定的,線程就像學(xué)生用一臺(tái)電腦開(kāi)多個(gè)爬蟲(chóng),爬蟲(chóng)數(shù)量由每臺(tái)電腦的運(yùn)行內(nèi)存決定。一個(gè)CPU可以有多個(gè)進(jìn)程,一個(gè)進(jìn)程有一個(gè)或多個(gè)線程。
多進(jìn)程1、導(dǎo)包
from multiprocessing import Process
2、寫(xiě)兩個(gè)任務(wù)也就是兩個(gè)函數(shù)
3、創(chuàng)建一個(gè)進(jìn)程進(jìn)程名字 = Process(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護(hù)進(jìn)程)
4、啟動(dòng)進(jìn)程進(jìn)程名字.start()
5、是否開(kāi)啟進(jìn)程守護(hù),一般主進(jìn)程會(huì)等待子進(jìn)程執(zhí)行完畢后再關(guān)閉程序。當(dāng)我們想程序主進(jìn)程跑完,直接銷(xiāo)毀掉未完成的子進(jìn)程,關(guān)閉程序的話(huà),加上一句代碼 :1.創(chuàng)建進(jìn)程的時(shí)候傳參數(shù)daemon=True2.進(jìn)程名字.daemon=True
6、進(jìn)程編號(hào)導(dǎo)包os獲取當(dāng)前進(jìn)程編號(hào)
os.getpid()
獲取當(dāng)前父進(jìn)程的編號(hào)
os.getppid()
代碼示例(未開(kāi)啟進(jìn)程守護(hù))
from multiprocessing import Processimport timeimport os# 一個(gè)寫(xiě)作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 打印當(dāng)前父進(jìn)程編號(hào)os.getppid() print('當(dāng)前父進(jìn)程編號(hào):', os.getppid()) print(name, '正在寫(xiě)作業(yè)...') time.sleep(0.2)# 一個(gè)打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 打印當(dāng)前父進(jìn)程編號(hào)os.getppid() print('當(dāng)前父進(jìn)程編號(hào):', os.getppid()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 進(jìn)程1寫(xiě)作業(yè) 元組傳參 p1 = Process(target=homeWork, args=('進(jìn)程1', 10)) # 進(jìn)程2打游戲 字典傳參 p2 = Process(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 啟動(dòng)進(jìn)程 p1.start() p2.start() time.sleep(1) print('主進(jìn)程結(jié)束---------------------------------------------')
未開(kāi)啟線程守護(hù)的運(yùn)行結(jié)果:
# 可以看到主進(jìn)程結(jié)束的,其子進(jìn)程還在繼續(xù)當(dāng)前進(jìn)程編號(hào): 14972當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束---------------------------------------------當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 5732當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 14752當(dāng)前父進(jìn)程編號(hào): 14972進(jìn)程2 正在打游戲...
Process finished with exit code 0
代碼示例(開(kāi)啟進(jìn)程守護(hù))
from multiprocessing import Processimport timeimport os# 一個(gè)寫(xiě)作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 打印當(dāng)前父進(jìn)程編號(hào)os.getppid() print('當(dāng)前父進(jìn)程編號(hào):', os.getppid()) print(name, '正在寫(xiě)作業(yè)...') time.sleep(0.2)# 一個(gè)打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 打印當(dāng)前父進(jìn)程編號(hào)os.getppid() print('當(dāng)前父進(jìn)程編號(hào):', os.getppid()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 打印當(dāng)前進(jìn)程編號(hào)os.getpid() print('當(dāng)前進(jìn)程編號(hào):', os.getpid()) # 進(jìn)程1寫(xiě)作業(yè) 元組傳參 第一種方法啟動(dòng)進(jìn)程守護(hù) p1 = Process(target=homeWork, args=('進(jìn)程1', 10), daemon=True) # 進(jìn)程2打游戲 字典傳參 p2 = Process(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 第二種 p2.daemon = True # 啟動(dòng)進(jìn)程 p1.start() p2.start() time.sleep(1) print('主進(jìn)程---------------------------------------------')
開(kāi)啟進(jìn)程守護(hù)的運(yùn)行結(jié)果
當(dāng)前進(jìn)程編號(hào): 372當(dāng)前進(jìn)程編號(hào): 10116當(dāng)前進(jìn)程編號(hào): 9860當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 9860當(dāng)前進(jìn)程編號(hào): 10116當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程2 正在打游戲...當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前進(jìn)程編號(hào): 9860當(dāng)前進(jìn)程編號(hào): 10116當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號(hào): 9860當(dāng)前進(jìn)程編號(hào): 10116當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程1 正在寫(xiě)作業(yè)...當(dāng)前父進(jìn)程編號(hào): 372進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束---------------------------------------------
Process finished with exit code 0
多線程1、導(dǎo)包
import threading
2、寫(xiě)兩個(gè)任務(wù)也就是兩個(gè)函數(shù)
3、創(chuàng)建一個(gè)線程線程名字 = threading.Thread(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護(hù)進(jìn)程)
4、啟動(dòng)線程線程名字.start()
5、是否開(kāi)啟線程守護(hù),一般當(dāng)前程序會(huì)等待子線程執(zhí)行完畢后再關(guān)閉程序。當(dāng)我們想程序跑完,銷(xiāo)毀掉未完成的子線程,直接關(guān)閉程序的話(huà),加上一句代碼 :1.創(chuàng)建線程的時(shí)候傳參數(shù)daemon=True2.線程名字.daemon=True
6、線程編號(hào)獲取當(dāng)前線程編號(hào)
threading.current_thread()
代碼示例(未開(kāi)啟進(jìn)程守護(hù))
import threadingimport time# 一個(gè)寫(xiě)作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在寫(xiě)作業(yè)...') time.sleep(0.2)# 一個(gè)打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫(xiě)作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=('進(jìn)程1', 10)) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 啟動(dòng)進(jìn)程 t1.start() t2.start() time.sleep(1) print('主進(jìn)程結(jié)束###################################################################################')
未開(kāi)啟線程守護(hù)的運(yùn)行結(jié)果
# 可以看到主進(jìn)程結(jié)束的,其線程還在繼續(xù)<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束###################################################################################<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-1, started 3364)><Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>進(jìn)程1 進(jìn)程2正在寫(xiě)作業(yè)... 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>
進(jìn)程2 進(jìn)程1 正在打游戲...正在寫(xiě)作業(yè)...
Process finished with exit code 0
代碼示例(開(kāi)啟線程守護(hù))
import threadingimport time# 一個(gè)寫(xiě)作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在寫(xiě)作業(yè)...') time.sleep(0.2)# 一個(gè)打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫(xiě)作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=('進(jìn)程1', 10), daemon=True) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) t2.daemon = True # 啟動(dòng)進(jìn)程 t1.start() t2.start() time.sleep(1) print('主進(jìn)程結(jié)束###################################################################################')
開(kāi)啟線程守護(hù)的運(yùn)行結(jié)果
<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-1, started daemon 15480)><Thread(Thread-2, started daemon 13700)>進(jìn)程1 進(jìn)程2 正在寫(xiě)作業(yè)...正在打游戲...
<Thread(Thread-2, started daemon 13700)><Thread(Thread-1, started daemon 15480)>
進(jìn)程1進(jìn)程2 正在寫(xiě)作業(yè)... 正在打游戲...
<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫(xiě)作業(yè)...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束###################################################################################
Process finished with exit code 0
到此這篇關(guān)于Python之多進(jìn)程與多線程的使用的文章就介紹到這了,更多相關(guān)Python 多進(jìn)程與多線程內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)2. 如何封裝一個(gè)Ajax函數(shù)3. ASP.NET MVC實(shí)現(xiàn)樹(shù)形導(dǎo)航菜單4. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)5. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲6. Ajax常用封裝庫(kù)——Axios的使用7. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))8. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera9. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?10. Python數(shù)據(jù)分析JupyterNotebook3魔法命令詳解及示例
