from multiprocessing import Manager,Process
class MyPro(Process):
def __init__(self,dic):
super().__init__()
self.dic = dic
def run(self):
self.dic[\'count\'] -= 1
print(self.dic)
if __name__ == \'__main__\':
m = Manager()
dic = m.dict({\'count\':100})
p = MyPro(dic)
p.start()
p.join()
print(\'主进程:\',dic)
from multiprocessing import Manager,Process,Lock
class MyPro(Process):
def __init__(self,dic,lock):
super().__init__()
self.dic = dic
self.lock = lock
def run(self):
self.lock.acquire()
self.dic[\'count\'] -= 1
self.lock.release()
# print(self.dic)
if __name__ == \'__main__\':
lock = Lock()
m = Manager()
dic = m.dict({\'count\':100})
p_list = []
for i in range(50):
p = MyPro(dic,lock)
p.start()
p_list.append(p)
for i in p_list:
p.join()
print(\'主进程:\',dic)