[莫烦python]进程锁

Multiprocessing 让你的多核计算机发挥真正潜力 (莫烦 Python 教程)


import multiprocessing as mp
import time


def job(v,num,l):
    l.acquire()
    for _ in range(10):
        time.sleep(0.1)
        v.value += num
        print(v.value)
    l.release()
    
def multicore():
    l = mp.Lock()
    v = mp.Value('i',0)
    p1 = mp.Process(target=job,args=(v,1,l))
    p2 = mp.Process(target=job,args=(v,3,l))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == '__main__':
    multicore()

你可能感兴趣的:([莫烦python]进程锁)