• Python 实现红绿灯


    一、通过Event来实现两个或多个线程间的交互,下面是一个红绿灯的例子,即起动一个线程做交通指挥信号灯,一个线程做车辆,车辆行驶按红灯停,绿灯行的规则。

    #!/usr/bin/python
    # -*- coding : utf-8 -*-
    # 作者: Presley
    # 时间: 2018-11-21
    # 邮箱:1209989516@qq.com
    # 这是我用来练习python线程锁的测试脚本
    
    import threading,time
    import random
    
    def light():
        if not event.isSet(): #如果没有设定event
            event.set() #wait就不阻塞 #绿灯状态
        count = 0
        while True:
            if count < 10:
                print("33[42;1m--green light on--33[0m")
            elif count <13:
                print("33[43;1m--yellow light on --33[0m")
            elif count <20:
                if event.isSet():
                    event.clear()
                print("33[41;1m--red light on-- 33[0m")
            else:
                count = 0
                event.set() #打开绿灯
            time.sleep(1)
            count +=1
    # def car(n):
    #     while 1:
    #         time.sleep(random.randrange(10))
    #         if event.isSet(): #绿灯
    #             print("car [%s] is running.." %n)
    #         else:
    #             print("car [%s] is waiting for the red light.." %n)
    def car(n):    #no bug version
        while 1:
            time.sleep(1) #让车慢点if event.isSet(): #绿灯
                print("car [%s] is running.." %n)
            else:
                print("car [%s] is waiting for the red light.." %n)
                event.wait() #不断检查flag有没有被设定,如果没有设定就等着,这个是输入event等待时间,可以精确到毫秒级
    if __name__ == "__main__":
        event = threading.Event()
        Light = threading.Thread(target=light)
        Light.start()
        for i in range(3):
            t = threading.Thread(target=car,args=(i,))
            t.start()

    执行结果:

    --green light on--
    --green light on--
    car [0] is running..
    --green light on--
    car [0] is running..
    --green light on--
    car [2] is running..
    --green light on--
    --green light on--
    --green light on--
    car [2] is running..
    --green light on--
    car [2] is running..
    --green light on--
    --green light on--
    car [1] is running..
    car [0] is running..
    --yellow light on --
    car [0] is running..
    --yellow light on --
    car [0] is running..
    --yellow light on --
    --red light on-- 
    --red light on-- 
    --red light on-- 
    car [1] is waiting for the red light..
    --red light on-- 
    car [0] is waiting for the red light..
    --red light on-- 
    car [2] is waiting for the red light..
    --red light on-- 
    --red light on-- 
    --green light on--
    --green light on--
    car [2] is running..
    --green light on--
    car [1] is running..
    --green light on--
    --green light on--
    car [1] is running..
    --green light on--
    car [0] is running..
    --green light on--
    --green light on--
    ...
  • 相关阅读:
    灵魂有香气的女子IOS版本APP,近期将考虑开放源代码
    PHP中$_SERVER获取当前页面的完整URL地址
    zabbix监控报错zabbix server is not running解决方法
    Linux重启inotify配置max_user_watches无效被恢复默认值8192的正确修改方法
    centos在yum install报错:Another app is currently holding the yum lock解决方法
    nginx去掉单个目录和多个目录PHP执行权限方法
    express搭建权限管理系统
    在express项目中使用formidable & multiparty实现文件上传
    vue生成图片验证码
    第0步:OracleRAC软件准备
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/9998128.html
Copyright © 2020-2023  润新知