python 进程锁

 

1.

#_*_coding:utf-8_*_
from  multiprocessing import Process,Lock
import os,time
def f(l,i):
    #加锁
    l.acquire()
    print('hello',i)
    #释放锁
    l.release()
if __name__ == '__main__':
    lock=Lock()
    for i in range(5):
           p=Process(target=f,args=(lock,i)).start()

输出

hello 4
hello 1
hello 2
hello 0
hello 3

 

posted @ 2019-01-29 12:21  anobscureretreat  阅读(233)  评论(0编辑  收藏  举报