想象在學(xué)校的一個機房,有固定數(shù)量的電腦,老師安排了一個爬蟲任務(wù)讓大家一起完成,每個學(xué)生使用一臺電腦爬取部分數(shù)據(jù),將數(shù)據(jù)放到一個公共數(shù)據(jù)庫。共同資源就像公共數(shù)據(jù)庫,進程就像每一個學(xué)生,每多一個學(xué)生,就多一個進程來完成這個任務(wù),機房里的電腦數(shù)量就像CPU,所以進程數(shù)量是CPU決定的,線程就像學(xué)生用一臺電腦開多個爬蟲,爬蟲數(shù)量由每臺電腦的運行內(nèi)存決定。
一個CPU可以有多個進程,一個進程有一個或多個線程。
1、導(dǎo)包
from multiprocessing import Process
2、寫兩個任務(wù)
也就是兩個函數(shù)
3、創(chuàng)建一個進程
進程名字 = Process(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護進程)
4、啟動進程
進程名字.start()
5、是否開啟進程守護,一般主進程會等待子進程執(zhí)行完畢后再關(guān)閉程序。當我們想程序主進程跑完,直接銷毀掉未完成的子進程,關(guān)閉程序的話,加上一句代碼 :
1.創(chuàng)建進程的時候傳參數(shù)daemon=True
2.進程名字.daemon=True
6、進程編號
導(dǎo)包os
獲取當前進程編號
os.getpid()
獲取當前父進程的編號
os.getppid()
代碼示例(未開啟進程守護)
from multiprocessing import Process import time import os # 一個寫作業(yè)函數(shù) def homeWork(name, count): for i in range(count): # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 打印當前父進程編號os.getppid() print("當前父進程編號:", os.getppid()) print(name, "正在寫作業(yè)...") time.sleep(0.2) # 一個打游戲函數(shù) def game(name, count): for i in range(count): # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 打印當前父進程編號os.getppid() print("當前父進程編號:", os.getppid()) print(name, "正在打游戲...") time.sleep(0.2) if __name__ == '__main__': # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 進程1寫作業(yè) 元組傳參 p1 = Process(target=homeWork, args=("進程1", 10)) # 進程2打游戲 字典傳參 p2 = Process(target=game, kwargs={"name": "進程2", "count": 10}) # 啟動進程 p1.start() p2.start() time.sleep(1) print("主進程結(jié)束---------------------------------------------")
未開啟線程守護的運行結(jié)果:
# 可以看到主進程結(jié)束的,其子進程還在繼續(xù)
當前進程編號: 14972
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
主進程結(jié)束---------------------------------------------
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...
當前進程編號: 5732
當前父進程編號: 14972
進程1 正在寫作業(yè)...
當前進程編號: 14752
當前父進程編號: 14972
進程2 正在打游戲...Process finished with exit code 0
代碼示例(開啟進程守護)
from multiprocessing import Process import time import os # 一個寫作業(yè)函數(shù) def homeWork(name, count): for i in range(count): # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 打印當前父進程編號os.getppid() print("當前父進程編號:", os.getppid()) print(name, "正在寫作業(yè)...") time.sleep(0.2) # 一個打游戲函數(shù) def game(name, count): for i in range(count): # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 打印當前父進程編號os.getppid() print("當前父進程編號:", os.getppid()) print(name, "正在打游戲...") time.sleep(0.2) if __name__ == '__main__': # 打印當前進程編號os.getpid() print("當前進程編號:", os.getpid()) # 進程1寫作業(yè) 元組傳參 第一種方法啟動進程守護 p1 = Process(target=homeWork, args=("進程1", 10), daemon=True) # 進程2打游戲 字典傳參 p2 = Process(target=game, kwargs={"name": "進程2", "count": 10}) # 第二種 p2.daemon = True # 啟動進程 p1.start() p2.start() time.sleep(1) print("主進程---------------------------------------------")
開啟進程守護的運行結(jié)果
當前進程編號: 372
當前進程編號: 10116
當前進程編號: 9860
當前父進程編號: 372
進程1 正在寫作業(yè)...
當前父進程編號: 372
進程2 正在打游戲...
當前進程編號: 9860
當前進程編號: 10116
當前父進程編號: 372
進程2 正在打游戲...
當前父進程編號: 372
進程1 正在寫作業(yè)...
當前進程編號: 9860
當前進程編號: 10116
當前父進程編號: 372
進程1 正在寫作業(yè)...
當前父進程編號: 372
進程2 正在打游戲...
當前進程編號: 9860
當前進程編號: 10116
當前父進程編號: 372
進程1 正在寫作業(yè)...
當前父進程編號: 372
進程2 正在打游戲...
主進程結(jié)束---------------------------------------------Process finished with exit code 0
1、導(dǎo)包
import threading
2、寫兩個任務(wù)
也就是兩個函數(shù)
3、創(chuàng)建一個線程
線程名字 = threading.Thread(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護進程)
4、啟動線程
線程名字.start()
5、是否開啟線程守護,一般當前程序會等待子線程執(zhí)行完畢后再關(guān)閉程序。當我們想程序跑完,銷毀掉未完成的子線程,直接關(guān)閉程序的話,加上一句代碼 :
1.創(chuàng)建線程的時候傳參數(shù)daemon=True
2.線程名字.daemon=True
6、線程編號
獲取當前線程編號
threading.current_thread()
代碼示例(未開啟進程守護)
import threading import time # 一個寫作業(yè)函數(shù) def homeWork(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, "正在寫作業(yè)...") time.sleep(0.2) # 一個打游戲函數(shù) def game(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, "正在打游戲...") time.sleep(0.2) if __name__ == '__main__': # 線程1寫作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=("進程1", 10)) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={"name": "進程2", "count": 10}) # 啟動進程 t1.start() t2.start() time.sleep(1) print("主進程結(jié)束###################################################################################")
未開啟線程守護的運行結(jié)果
# 可以看到主進程結(jié)束的,其線程還在繼續(xù)
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
主進程結(jié)束###################################################################################
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-1, started 3364)>
Thread(Thread-2, started 9100)>
進程2 正在打游戲...進程1
正在寫作業(yè)...
Thread(Thread-1, started 3364)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started 9100)>
進程2 正在打游戲...
Thread(Thread-2, started 9100)>Thread(Thread-1, started 3364)>
進程1
進程2正在寫作業(yè)...
正在打游戲...
Thread(Thread-2, started 9100)>Thread(Thread-1, started 3364)>進程2 進程1 正在打游戲...
正在寫作業(yè)...Process finished with exit code 0
代碼示例(開啟線程守護)
import threading import time # 一個寫作業(yè)函數(shù) def homeWork(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, "正在寫作業(yè)...") time.sleep(0.2) # 一個打游戲函數(shù) def game(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, "正在打游戲...") time.sleep(0.2) if __name__ == '__main__': # 線程1寫作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=("進程1", 10), daemon=True) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={"name": "進程2", "count": 10}) t2.daemon = True # 啟動進程 t1.start() t2.start() time.sleep(1) print("主進程結(jié)束###################################################################################")
開啟線程守護的運行結(jié)果
Thread(Thread-1, started daemon 15480)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started daemon 13700)>
進程2 正在打游戲...
Thread(Thread-2, started daemon 13700)>
進程2 正在打游戲...
Thread(Thread-1, started daemon 15480)>
進程1 正在寫作業(yè)...
Thread(Thread-1, started daemon 15480)>Thread(Thread-2, started daemon 13700)>
進程1
進程2 正在寫作業(yè)...正在打游戲...Thread(Thread-2, started daemon 13700)>Thread(Thread-1, started daemon 15480)>
進程1進程2 正在寫作業(yè)... 正在打游戲...
Thread(Thread-1, started daemon 15480)>
進程1 正在寫作業(yè)...
Thread(Thread-2, started daemon 13700)>
進程2 正在打游戲...
主進程結(jié)束###################################################################################Process finished with exit code 0
到此這篇關(guān)于Python之多進程與多線程的使用的文章就介紹到這了,更多相關(guān)Python 多進程與多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標簽:淘寶好評回訪 濟源 阜新 興安盟 隨州 昭通 合肥 信陽
巨人網(wǎng)絡(luò)通訊聲明:本文標題《Python之多進程與多線程的使用》,本文關(guān)鍵詞 Python,之多,進程,與,多,線程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。