多行程與多執行緒
- 行程與執行緒
- 多行程
- 多執行緒
行程與執行緒
想象在學校的一個機房,有固定數量的電腦,老師安排了一個爬蟲任務讓大家一起完成,每個學生使用一臺電腦爬取部分資料,將資料放到一個公共資料庫,共同資源就像公共資料庫,行程就像每一個學生,每多一個學生,就多一個行程來完成這個任務,機房里的電腦數量就像CPU,所以行程數量是CPU決定的,執行緒就像學生用一臺電腦開多個爬蟲,爬蟲數量由每臺電腦的運行記憶體決定,
一個CPU可以有多個行程,一個行程有一個或多個執行緒,
多行程
1、導包
from multiprocessing import Process
2、寫兩個任務
也就是兩個函式
3、創建一個行程
行程名字 = Process(target=函式名字,函式引數傳字典或元組,是否守護行程)
4、啟動行程
行程名字.start()
5、是否開啟行程守護,一般主行程會等待子行程執行完畢后再關閉程式,當我們想程式主行程跑完,直接銷毀掉未完成的子行程,關閉程式的話,加上一句代碼 :
1.創建行程的時候傳引數daemon=True
2.行程名字.daemon=True
6、行程編號
導包os
獲取當前行程編號
os.getpid()
獲取當前父行程的編號
os.getppid()
代碼示例(未開啟行程守護)
from multiprocessing import Process
import time
import 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 Process
import time
import 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=True
2.執行緒名字.daemon=True
6、執行緒編號
獲取當前執行緒編號
threading.current_thread()
代碼示例(未開啟行程守護)
import threading
import 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 threading
import 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

原創不易,請給博主一個小小的贊吧~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/257473.html
標籤:python
上一篇:python 資料抓取三種方法
