互斥锁

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

  

上一篇:几种嵌入式操作系统对比


下一篇:REDIS缓存穿透,缓存击穿,缓存雪崩原因+解决方案