Python之多進程與多線程的使用
想象在學校的一個機房,有固定數量的電腦,老師安排了一個爬蟲任務讓大家一起完成,每個學生使用一臺電腦爬取部分數據,將數據放到一個公共數據庫。共同資源就像公共數據庫,進程就像每一個學生,每多一個學生,就多一個進程來完成這個任務,機房里的電腦數量就像CPU,所以進程數量是CPU決定的,線程就像學生用一臺電腦開多個爬蟲,爬蟲數量由每臺電腦的運行內存決定。一個CPU可以有多個進程,一個進程有一個或多個線程。
多進程1、導包
from multiprocessing import Process
2、寫兩個任務也就是兩個函數
3、創建一個進程進程名字 = Process(target=函數名字,函數參數傳字典或元組,是否守護進程)
4、啟動進程進程名字.start()
5、是否開啟進程守護,一般主進程會等待子進程執行完畢后再關閉程序。當我們想程序主進程跑完,直接銷毀掉未完成的子進程,關閉程序的話,加上一句代碼 :1.創建進程的時候傳參數daemon=True2.進程名字.daemon=True
6、進程編號導包os獲取當前進程編號
os.getpid()
獲取當前父進程的編號
os.getppid()
代碼示例(未開啟進程守護)
from multiprocessing import Processimport timeimport os# 一個寫作業函數def homeWork(name, count): for i in range(count): # 打印當前進程編號os.getpid() print('當前進程編號:', os.getpid()) # 打印當前父進程編號os.getppid() print('當前父進程編號:', os.getppid()) print(name, '正在寫作業...') time.sleep(0.2)# 一個打游戲函數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寫作業 元組傳參 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('主進程結束---------------------------------------------')
未開啟線程守護的運行結果:
# 可以看到主進程結束的,其子進程還在繼續當前進程編號: 14972當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...主進程結束---------------------------------------------當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...當前進程編號: 5732當前父進程編號: 14972進程1 正在寫作業...當前進程編號: 14752當前父進程編號: 14972進程2 正在打游戲...
Process finished with exit code 0
代碼示例(開啟進程守護)
from multiprocessing import Processimport timeimport os# 一個寫作業函數def homeWork(name, count): for i in range(count): # 打印當前進程編號os.getpid() print('當前進程編號:', os.getpid()) # 打印當前父進程編號os.getppid() print('當前父進程編號:', os.getppid()) print(name, '正在寫作業...') time.sleep(0.2)# 一個打游戲函數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寫作業 元組傳參 第一種方法啟動進程守護 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('主進程---------------------------------------------')
開啟進程守護的運行結果
當前進程編號: 372當前進程編號: 10116當前進程編號: 9860當前父進程編號: 372進程1 正在寫作業...當前父進程編號: 372進程2 正在打游戲...當前進程編號: 9860當前進程編號: 10116當前父進程編號: 372進程2 正在打游戲...當前父進程編號: 372進程1 正在寫作業...當前進程編號: 9860當前進程編號: 10116當前父進程編號: 372進程1 正在寫作業...當前父進程編號: 372進程2 正在打游戲...當前進程編號: 9860當前進程編號: 10116當前父進程編號: 372進程1 正在寫作業...當前父進程編號: 372進程2 正在打游戲...主進程結束---------------------------------------------
Process finished with exit code 0
多線程1、導包
import threading
2、寫兩個任務也就是兩個函數
3、創建一個線程線程名字 = threading.Thread(target=函數名字,函數參數傳字典或元組,是否守護進程)
4、啟動線程線程名字.start()
5、是否開啟線程守護,一般當前程序會等待子線程執行完畢后再關閉程序。當我們想程序跑完,銷毀掉未完成的子線程,直接關閉程序的話,加上一句代碼 :1.創建線程的時候傳參數daemon=True2.線程名字.daemon=True
6、線程編號獲取當前線程編號
threading.current_thread()
代碼示例(未開啟進程守護)
import threadingimport time# 一個寫作業函數def homeWork(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, '正在寫作業...') time.sleep(0.2)# 一個打游戲函數def game(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫作業 元組傳參 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('主進程結束###################################################################################')
未開啟線程守護的運行結果
# 可以看到主進程結束的,其線程還在繼續<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-2, started 9100)>進程2 正在打游戲...主進程結束###################################################################################<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-1, started 3364)><Thread(Thread-2, started 9100)>進程2 正在打游戲...進程1 正在寫作業...<Thread(Thread-1, started 3364)>進程1 正在寫作業...<Thread(Thread-2, started 9100)>進程2 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>進程1 進程2正在寫作業... 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>
進程2 進程1 正在打游戲...正在寫作業...
Process finished with exit code 0
代碼示例(開啟線程守護)
import threadingimport time# 一個寫作業函數def homeWork(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, '正在寫作業...') time.sleep(0.2)# 一個打游戲函數def game(name, count): for i in range(count): # 打印當前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫作業 元組傳參 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('主進程結束###################################################################################')
開啟線程守護的運行結果
<Thread(Thread-1, started daemon 15480)>進程1 正在寫作業...<Thread(Thread-2, started daemon 13700)>進程2 正在打游戲...<Thread(Thread-2, started daemon 13700)>進程2 正在打游戲...<Thread(Thread-1, started daemon 15480)>進程1 正在寫作業...<Thread(Thread-1, started daemon 15480)><Thread(Thread-2, started daemon 13700)>進程1 進程2 正在寫作業...正在打游戲...
<Thread(Thread-2, started daemon 13700)><Thread(Thread-1, started daemon 15480)>
進程1進程2 正在寫作業... 正在打游戲...
<Thread(Thread-1, started daemon 15480)>進程1 正在寫作業...<Thread(Thread-2, started daemon 13700)>進程2 正在打游戲...主進程結束###################################################################################
Process finished with exit code 0
到此這篇關于Python之多進程與多線程的使用的文章就介紹到這了,更多相關Python 多進程與多線程內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
