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("\033[42;1m--green light on--\033[0m")
elif count <13:
print("\033[43;1m--yellow light on --\033[0m")
elif count <20:
if event.isSet():
event.clear()
print("\033[41;1m--red light on-- \033[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--
...
Python 实现红绿灯的更多相关文章
- 【python】-- 信号量(Semaphore)、event(红绿灯例子)
信号量(Semaphore) 之前讲的线程锁(互斥锁) 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里 ...
- Python并发编程-事件,红绿灯控制
事件用来控制多个进程同时执行或者阻塞 set和clear 分别用来修改一个事件的状态,True或者False is_set 用来查看一个事件的状态 wait 是依据事件的状态来决定自己是否在wait处 ...
- python多线程Event实现红绿灯案例
代码: # __author__ = 'STEVEN' # coding = utf-8 import time,threading #开启事件 event = threading.Event() c ...
- python学习笔记-进程线程
1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...
- Python之路第一课Day9--随堂笔记之二(进程、线程、协程篇)
本节内容 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者消费者模型 Queu ...
- python之进程与线程
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- Python Day9
Paramiko模块 paramiko模块基于SSH用于连接远程服务器并执行相关操作 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHCl ...
- python学习道路(day10note)(线程,进程)
1.计算机的发展史 看alex的博客吧,了解一下可以了 2.线程与GIL简介 #线程 #一道单一的指令的控制流,寄生在进程中 #单一进程里的多个线程是共享数据的 #多个线程涉及修改共享数据的时候需要枷 ...
- Python之路,Day9, 进程、线程、协程篇
本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者 ...
随机推荐
- MySQL配置说明
以下内容,来源于http://www.jb51.net/article/48082.htm [client] port = 3306 socket = /tmp/mysql.sock [mysqld] ...
- YUV的数据格式
一.YUV格式分为两大类:planar(平面)和packed(打包).planar格式,先连续存储所有像素点的Y分量,紧接着存储所有像素点的U,随后存储所有像素点的V.packed格式,每个像素点的Y ...
- Light OJ 1009
题意: 给你一个二分图, (可能不连通) 求可能多的子集元素个数: 思路: 直接DFS 给二分图染色就有了, 统计联通块中个数, 去最大值相加即可. #include<bits/stdc++.h ...
- 解决FTPClient上传文件为空,显示0字节
JAVA使用FTPClient上传文件时总是为空,而使用FileZilla客户端时却不会. 后来查了下资料,FTP服务器有被动模式和主动模式.(具体查另外资料) 在JAVA中将FTPClient设置为 ...
- mysql定时任务,每天的零点执行一个存储过程
1 前言 利用navicat工具来写存储过程及定时执行,此文章是按照自身经验总结的,仅作为记录使用. 2 步骤 2.1 新建过程 2.2 在函数体写你需要执行的代码 CREATE DEFINER=`r ...
- [PHP]curl上传多文件
码一下curl上传多文件的行 5.5之前版本的写法 $file = array( 'pic[0]'=>"@E:\\wwwroot\\10003\\temp_56.ini;type=te ...
- 重启报错:Failed to open /dev/initctl: No such device or address
[root@WEB-APP-REP-MASTER ~]# rebootError getting authority: Error initializing authority: Error call ...
- 前端PS切图
http://www.imooc.com/learn/506 慕课网地址 Tools Tools Photoshop 快捷键 l 移动工具 V l 选取工具 M l 套索工具 L l ...
- swift 学习- 19 -- 可选链式调用
// 可选链式调用 是一种在当前值可能为 nil 的可选值上请求 和 调用属性, 方法以及下标, 如果 可选值有值, 那么调用就会成功, 如果可选值是 nil, 那么就会将返回 nil , // 多个 ...
- Confluence 6 使用主题
主题是被用来修改 Confluence 站点或空间的外观的. Confluence 安装了一个单一的默认主题,或者你也可以下载和安装其他的主题.你可以从 The Atlassian Marketpla ...