06 数据共享

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'])

 

posted on 2019-01-14 15:20  =.===  阅读(77)  评论(0编辑  收藏  举报

导航