程序與程序之間資料是隔離的
from multiprocessing import執行結果:process
deffunc(lst, name):
(name, lst)
if__name__ == '
__main__':
lst = [1, 2]
p1 = process(target=func, args=(lst, "
程序1"
)) p2 = process(target=func, args=(lst, "
程序2"
)) p1.start()
p2.start()
p1.join()
p2.join()
print("
主程序結束
", lst)
程序1 [1, 2, 66]程序2 [1, 2, 66]
主程序結束 [1, 2]
global來演示程序之間資料是隔離的
from multiprocessing import執行結果:process
deffunc(name):
print(f"開始"
)
global
n n =0
print(f"
結束,n=")
if__name__ == '
__main__':
n = 100p1 = process(target=func, args=("
程序1"
,)) p2 = process(target=func, args=("
程序2"
,)) p1.start()
p2.start()
p1.join()
p2.join()
print(f"
主程序結束,n=
")
程序1開始再看個會報錯的程式程序1結束,n=0
程序2開始
程序2結束,n=0
主程序結束,n=100
from multiprocessing import執行結果:process
deffunc(name):
print(f"開始"
)
global
n
print(f"n="
) n =0
print(f"
結束,n=")
if__name__ == '
__main__':
n = 100p1 = process(target=func, args=("
程序1"
,)) p2 = process(target=func, args=("
程序2"
,)) p1.start()
p2.start()
p1.join()
p2.join()
print(f"
主程序結束,n=
")
程序1開始通過對比可以發現,即使使用了global引用全域性變數n,子程序裡也沒有成功的引入進來。process process-1:
traceback (most recent call last):
file
"g:\python3.6\lib\multiprocessing\process.py
", line 258, in
_bootstrap
self.run()
file
"g:\python3.6\lib\multiprocessing\process.py
", line 93, in
run self._target(*self._args, **self._kwargs)
file
"g:\python28\code\day08\整理筆記\已記錄\程序與程序之間資料是隔離的\222.py
", line 7, in
func
print(f"n="
)nameerror: name 'n
'isnotdefined
程序2開始
process process-2:
traceback (most recent call last):
file
"g:\python3.6\lib\multiprocessing\process.py
", line 258, in
_bootstrap
self.run()
file
"g:\python3.6\lib\multiprocessing\process.py
", line 93, in
run self._target(*self._args, **self._kwargs)
file
"g:\python28\code\day08\整理筆記\已記錄\程序與程序之間資料是隔離的\222.py
", line 7, in
func
print(f"n="
)nameerror: name 'n
'isnotdefined
主程序結束,n=100
python3 程序之間資料是隔離的
程序之間資料是隔離的,相當於兩家公司之間是隔離的 from multiprocessing import process deffunc lst,name print name,lst if name main lst 1,2 p1 process target func,args lst,程序1 ...
c 本地程序之間進行通訊
c 程序之間進行通訊 介面 namespace 程序通訊 dllimport user32.dll public static extern intsendmessage int hwnd,int msg,int wparam,refcopydatastruct lparam dllimport u...
python3 殭屍程序
coding utf 8 from multiprocessing import process import time,os defrun print 子 os.getpid if name main p process target run p.start print 主 os.getpid t...