- 1.進(jìn)程
Process進(jìn)程類的說明 - Process([group [, target [, name [, args [, kwargs]]]]])
group:指定進(jìn)程組烛恤,目前只能使用None
target:執(zhí)行的目標(biāo)任務(wù)名
name:進(jìn)程名字
args:以元組方式給執(zhí)行任務(wù)傳參
kwargs:以字典方式給執(zhí)行任務(wù)傳參 - Process創(chuàng)建的實(shí)例對象的常用方法:
start():啟動子進(jìn)程實(shí)例(創(chuàng)建子進(jìn)程)
join():等待子進(jìn)程執(zhí)行結(jié)束
terminate():不管任務(wù)是否完成祖娘,立即終止子進(jìn)程 - Process創(chuàng)建的實(shí)例對象的常用屬性:
name:當(dāng)前進(jìn)程的別名,默認(rèn)為Process-N较剃,N為從1開始遞增的整數(shù)
import multiprocessing
import os
import time
def dance():
for i in range(5):
print("dance", os.getpid())
print("dance", multiprocessing.current_process())
print("1.跳舞中>>>")
time.sleep(0.2)
def sing():
for i in range(5):
# 獲取當(dāng)前進(jìn)程編號
print("sing", os.getpid())
# 獲取父進(jìn)程編號
print("sing_fater",os.getppid())
# 獲取當(dāng)前進(jìn)程
print("sing", multiprocessing.current_process())
print("2.唱歌中>>>")
time.sleep(0.2)
if __name__ == '__main__':
print("main", os.getpid())
print("main", multiprocessing.current_process())
dance_process = multiprocessing.Process(target=dance)
sing_process = multiprocessing.Process(target=sing)
dance_process.start()
sing_process.start()
打印
main 5751
main <_MainProcess(MainProcess, started)>
dance 5752
dance <Process(Process-1, started)>
1.跳舞中>>>
sing 5753
sing_fater 5751
sing <Process(Process-2, started)>
2.唱歌中>>>
dance 5752
dance <Process(Process-1, started)>
1.跳舞中>>>
sing 5753
sing_fater 5751
sing <Process(Process-2, started)>
2.唱歌中>>>
dance 5752
dance <Process(Process-1, started)>
1.跳舞中>>>
sing 5753
sing_fater 5751
sing <Process(Process-2, started)>
2.唱歌中>>>
dance 5752
dance <Process(Process-1, started)>
1.跳舞中>>>
sing 5753
sing_fater 5751
sing <Process(Process-2, started)>
2.唱歌中>>>
dance 5752
dance <Process(Process-1, started)>
1.跳舞中>>>
sing 5753
sing_fater 5751
sing <Process(Process-2, started)>
2.唱歌中>>>
注意:進(jìn)程之間不共享全局變量的小結(jié)
創(chuàng)建子進(jìn)程會對主進(jìn)程資源進(jìn)行拷貝,也就是說子進(jìn)程是主進(jìn)程的一個(gè)副本技健,好比是一對雙胞胎写穴,之所以進(jìn)程之間不共享全局變量,是因?yàn)椴僮鞯牟皇峭粋€(gè)進(jìn)程里面的全局變量雌贱,只不過不同進(jìn)程里面的全局變量名字相同而已
為了保證子進(jìn)程能夠正常的運(yùn)行啊送,主進(jìn)程會等所有的子進(jìn)程執(zhí)行完成以后再銷毀,設(shè)置守護(hù)主進(jìn)程的目的是主進(jìn)程退出子進(jìn)程銷毀欣孤,不讓主進(jìn)程再等待子進(jìn)程去執(zhí)行馋没。
設(shè)置守護(hù)主進(jìn)程方式: 子進(jìn)程對象.daemon = True
銷毀子進(jìn)程方式: 子進(jìn)程對象.terminate()
- 2.線程
1.線程之間執(zhí)行是無序的
2.主線程會等待所有的子線程執(zhí)行結(jié)束再結(jié)束
3.線程之間共享全局變量
4.線程之間共享全局變量數(shù)據(jù)出現(xiàn)錯(cuò)誤問題
import time
# 定義全局變量
my_list = list()
# 寫入數(shù)據(jù)任務(wù)
def write_data():
for i in range(5):
my_list.append(i)
time.sleep(0.1)
print("write_data:", my_list)
# 讀取數(shù)據(jù)任務(wù)
def read_data():
time.sleep(0.2)
print("read_data:", my_list)
if __name__ == '__main__':
# 創(chuàng)建寫入數(shù)據(jù)的線程
write_thread = threading.Thread(target=write_data)
# 創(chuàng)建讀取數(shù)據(jù)的線程
read_thread = threading.Thread(target=read_data)
# read_thread.start()
write_thread.start()
# 延時(shí)
# time.sleep(1)
# 主線程等待寫入線程執(zhí)行完成以后代碼在繼續(xù)往下執(zhí)行
# write_thread.join()
print("開始讀取數(shù)據(jù)啦")
read_thread.start()```
```開始讀取數(shù)據(jù)啦
read_data: [0, 1]
write_data: [0, 1, 2, 3, 4]```