Python 多线程编程-04-threading 模块 - Event

目   录

1. threading.Condition 介绍

1.1 threading.Event 机制

1.2 threading.Event 属性和方法

2. threading.Condition 使用示范


Python 多线程编程目录

Python 多线程编程-01-threading 模块初识

Python 多线程编程-02-threading 模块-锁的使用

Python 多线程编程-03-threading 模块 - Condition

Python 多线程编程-04-threading 模块 - Event

Python 多线程编程-05-threading 模块 - Semaphore 和 BoundedSemaphore

Python 多线程编程-06-threading 模块 - Timer

Python 多线程编程-07-threading 模块 - Barrier

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 属性和方法
序号属性和方法描述
1clear()清除 Event 对象内部的信号标志,即设置为 False。
2is_set()/isSet()判断内部的信号标志的状态。当使用 set()后,isSet()方法返回True;当使用 clear()后,isSet() 方法返回 False。
3set()设置 Event 对象内部的信号标志为 True。
4wait()该方法只有在内部信号为 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

运行结果如下:

'''

要是大家觉得写得还行,麻烦点个赞或者收藏吧,想个博客涨涨人气,非常感谢!

'''

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南野栀子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值