from multiprocessing import Event,Process
from time import sleep
def wait_event():
print("process1也想操作臨界區,但是要阻塞等待主行程")
e.wait()
print("主行程操作完了,到我了",e.is_set())#True
def wait_event_timeout():
print("process2也想操作臨界區,但是要阻塞等待主行程")
e.wait(2)
# e.wait()
print("我不等了",e.is_set())#false
e = Event()
p1 = Process(name = "block",target = wait_event)
p2 = Process(name = "non-block",target = wait_event_timeout)
p1.start()
p2.start()
print("假設正在忙碌的操作臨界資源")
sleep(3)
e.set()
print("主行程操作完了,開放臨界區")
p1.join()
p2.join()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181829.html
標籤:Python
上一篇:Python—行程間通信
下一篇:協程—概念以及基本使用
