zoukankan      html  css  js  c++  java
  • 40_并发编程-事件

    一、基本定义
        
      事件默认值为False
      event = Event() #设置一个事件实例
      event.set() #设置标志位,将e事件的状态改为True
      event.clear() #清空标志位,将e事件的状态改为False
      event.wait()  #等待设置标志位
     
    二、信号灯实例
     1 import time
     2 import random
     3 from multiprocessing import Process,Event
     4 
     5 #模拟红绿灯执行状态的函数
     6 def traffic_lights(e):
     7     while 1:
     8         print('红灯啦')
     9         time.sleep(5)
    10         e.set()
    11         print('绿灯亮')
    12         time.sleep(3)
    13         e.clear()  #将e改为了False
    14 def car(i,e):
    15 
    16     if not e.is_set(): #新来的车看到是红灯
    17         print('我们在等待.....')
    18         e.wait()
    19         print('走你')
    20     else:
    21         print('可以走了!!!')
    22 
    23 if __name__ == '__main__':
    24     e = Event()
    25     hld = Process(target=traffic_lights,args=(e,))
    26     hld.start()
    27     while 1:
    28         time.sleep(0.5)
    29         #创建10个车
    30         for i in range(3):
    31             # time.sleep(random.randrange(1,3))
    32             p1 = Process(target=car,args=(i,e,))
    33             p1.start()
    信号灯实例

     

  • 相关阅读:
    9.jQuery工具方法
    8.jQuery遍历索引的方法
    7.jQuery中位置坐标图形相关方法
    CentOS安装log.io
    centos7搭建frps内网穿透服务
    docker showdoc安装
    【测试基础】等价类、边界值的概念及划分
    3-14 Pandas绘图
    3-13 索引进阶
    3-12 字符串操作
  • 原文地址:https://www.cnblogs.com/hq82/p/9851583.html
Copyright © 2011-2022 走看看