互斥锁
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!!!!!!!!')
本文来自博客园,作者:一石数字欠我15w!!!,转载请注明原文链接:https://www.cnblogs.com/52-qq/p/7428853.html