Python 数据共享

import time
from multiprocessing import Process,Manager,Lock # a = 10
# # tmp = a
# # tmp -= 1
# # a = tmp
# a -= 1 # a = a - 1 def f1(m_d,l2):
# m_d['num'] -= 1 #
with l2:
# l2.acquire()
tmp = m_d['num']
tmp -= 1
time.sleep(0.1)
m_d['num'] = tmp
# l2.release() if __name__ == '__main__':
m = Manager()
l2 = Lock()
m_d = m.dict({'num':100})
p_list = []
for i in range(10):
p = Process(target=f1,args=(m_d,l2))
p.start()
p_list.append(p) [pp.join() for pp in p_list] print(m_d['num'])
上一篇:基于python中jieba包的中文分词中详细使用之一


下一篇:不停的实例化对象导致OOM