目   录

1. threading.Condition 介绍

1.1 threading.Event 机制

1.2 threading.Event 属性和方法

2. threading.Condition 使用示范


1. threading.Condition 介绍

1.1 threading.Event 机制

        在 Python 多线程编程-03-threading 模块 - Condition 中介绍了生产者-消费者模式的代码实现,使用了 threading.Condition 来控制同一个资源池的使用,其中的生产者线程和消费者线程是对等的,没有什么主从之分。而 threading.Event 机制类似于一个线程向其它多个线程发号施令的模式,其它线程都会持有一个threading.Event 的对象,这些线程都会等待这个事件的“发生”,如果此事件一直不发生,那么这些线程将会阻塞,直至事件的“发生”。这种场景是非常普遍的。

1.2 threading.Event 构造方法

       构造方法非常简单,没有参数。

event=threading.Event()

1.3 threading.Event 属性和方法

threading.Event 属性和方法

序号

属性和方法

描述

1

clear()

清除 Event 对象内部的信号标志,即设置为 False。

2

is_set()/isSet()

判断内部的信号标志的状态。当使用 set()后,isSet()方法返回True;当使用 clear()后,isSet() 方法返回 False。

3

set()

设置 Event 对象内部的信号标志为 True。

4

wait()

该方法只有在内部信号为 True 的时候才会被执行并完成返回。当内部信号标志为False 时,则 wait() 一直等待到其为 True 时才返回。

2. threading.Event 使用示范

# 设计一个红路灯,threading.Event 是汽车方向的指示
# 在10秒的时间内,4 秒之前是红灯,4秒到8秒是绿灯,后面又是红灯

# 设计一个汽车类,汽车方向的红绿灯,绿灯(set True)时候通行,红灯(set False)等待
# 最多等待 10秒,然后就掉头;其中 timer 用于指示其第几秒来到红路灯路口

# 设计一个行人类,汽车方向的红绿灯,绿灯(set True)时候等待,红灯(set False)通行
# 最多等待 10秒,然后就掉头;其中 timer 用于指示其第几秒来到红路灯路口

%%time

import threading
import time

# 设计一个红路灯,threading.Event 是汽车方向的指示
# 在10秒的时间内,4 秒之前是红灯,4秒到8秒是绿灯,后面又是红灯

# 设计一个汽车类,汽车方向的红绿灯,绿灯(set True)时候通行,红灯(set False)等待
# 最多等待 10秒,然后就掉头;其中 timer 用于指示其第几秒来到红路灯路口
class Car(threading.Thread):
    def __init__(self,color,event,timer):
        super().__init__()
        self.color=color
        self.event=event
        self.timer=timer
        
    
    def run(self):
        time.sleep(self.timer)
        count=0
        while(count<11):
            if(self.event.is_set()):
                print(time.ctime(),"The car of {0} will thransfer ".format(self.color))
                time.sleep(1)
                break
            else:
                print(time.ctime(),"The car of {0} is waiting! ".format(self.color))
                self.event.wait(10)
                count+=1
        if(count>=11):
            print(time.ctime(),"The car of {0} is gone! ".format(self.color))

# 设计一个行人类,汽车方向的红绿灯,绿灯(set True)时候等待,红灯(set False)通行
# 最多等待 10秒,然后就掉头;其中 timer 用于指示其第几秒来到红路灯路口
class Man(threading.Thread):
    def __init__(self,name,event,timer):
        super().__init__()
        self.name=name
        self.event=event
        self.timer=timer
        
    
    def run(self):
        time.sleep(self.timer)
        count=0
        while(count<11):
            if(not self.event.is_set()):
                print(time.ctime(),"The man  {0} will thransfer ".format(self.name))
                break
            else:
                print(time.ctime(),"The man  {0} is waiting! ".format(self.name))
                time.sleep(1)
                count+=1
        if(count>=11):
            print(time.ctime(),"The man  {0} is gone! ".format(self.name))
                
if __name__=="__main__":
    light=threading.Event()
    ada=Man("ada",light,1)
    ivy=Man("ivy",light,5)
    allen=Man("allen",light,8)
    jessica=Man("jessica",light,9)
    bluecar=Car("blue",light,2)
    yellowcar=Car("yellow",light,7)
    whitecar=Car("white",light,9)
    count=0

    ada.start()
    ivy.start()
    allen.start()
    jessica.start()
    bluecar.start()
    yellowcar.start()
    whitecar.start()
    
    while(count<10):
        if(count==4):
            light.set()
        if(count==8):
            light.clear()
            
        if(light.is_set()):
            print("{0} the light is {1}".format(time.ctime(),"Green"))
        else:
            print("{0} the light is {1}".format(time.ctime(),"Red"))
        time.sleep(1)
        count+=1

运行结果如下:

python创建的thread会自动销毁吗_多线程编程