互斥锁

from multiprocessing import Process,Lock
import os,time
def walk(mutex):
    mutex.acquire()           #为了防止偷看,记得上锁
    print('task[%s] 洗澡'%os.getpid())
    time.sleep(2)
    print('task[%s] 洗完澡'%os.getpid())
    mutex.release()           #洗完澡打开锁,难不成待一辈子

if __name__ == '__main__':
    mutex = Lock()
    p1 = Process(target=walk,args=(mutex,))
    p2 = Process(target=walk,args=(mutex,))
    p3 = Process(target=walk,args=(mutex,))

    p1.start()
    p2.start()
    p3.start()

    print('this is father!!!!!!!!')

  

posted @ 2017-08-25 17:00  一石数字欠我15w!!!  阅读(444)  评论(0编辑  收藏  举报