线程同步方法-Condition
介绍
Contion是Python线程同步的一种方法,也是Python中最复杂的线程同步的方式之一,相比Lock
,Condition可以通过特定事件触发,比起Lock
更加灵活。Condition内部实际上还是使用Rlock和Lock实现的。
Conditon的常用方法 wait
,wait_for
,notify
,notify_all
wait
:等待条件通知
wait_for
:等待某个条件的通知
notify
:唤醒一个或多个正在等待的condtion
notify_all
:唤醒所有正在等待的condtion
使用Condtion实现两个线程通信例子
使用condition让两个线程交互pirnt
class XiaoAi(threading.Thread):
def __init__(self, cond):
super().__init__(name="小爱")
self.cond = cond
def run(self):
with self.cond:
self.cond.wait()
print("{} : 在 ".format(self.name))
self.cond.notify(</