Multiprocessing 学会多进程 (莫烦 Python 教程)笔记-4-lock锁
lock锁可保持在多线程运行环境中,单线程的独立
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()