Interprocess data are independent, by means of a queue or pipeline communicate, both of which are based on message passing
though between processes data independently, but the data sharing may be achieved through the Manager, the Manager function in fact much more than that
# coding:utf-8
import time
from multiprocessing import Process, Lock, Manager
def work(d, lock):
with lock:
d['count'] -= 1
if __name__ == '__main__':
start_time = time.time()
lock = Lock()
with Manager() as m:
dic = m.dict({"count": 100})
print(dic)
p_l = []
for i in range(100):
p = Process(target=work, args=(dic, lock))
p_l.append(p)
p.start()
for p in p_l:
p.join()
print(dic)
end_time = time.time()
print("程序执行的时间:", end_time - start_time)
s_time = time.time()
dic2 = {"count": 100}
p_lst = []
forI in Range (100 ):
P = Process (target = Work, args = (DIC2, Lock))
p_lst.append (P)
p.start ()
for P in p_lst:
p.join ()
e_time = the time.time ( )
Print (DIC2)
Print ( " program execution time: " , e_time - S_TIME)
# { 'COUNT': 100}
# { 'COUNT': 0}
# program execution time: 12.078125
# { 'COUNT': 100}
# program execution time: 11.375