05. 多线程之事件(Event类)

  1. python线程的事件用于主线程控制其他线程的执行,事件是一个简单的线程同步对象,其主要提供以下几个方法:
  • clear 将flag设置为“False”
  • set 将flag设置为“True”
  • is_set 判断是否设置了flag
  • wait 会一直监听flag,如果没有检测到flag就一直处于阻塞状态
  1. 事件处理的机制:全局定义了一个“Flag”,当flag值为“False”,那么event.wait()就会阻塞,当flag值为“True”,那么event.wait()便不再阻塞。
#!/usr/bin/env python
# coding:utf-8

import threading
import time

event = threading.Event()


def lighter():
    count = 0
    event.set()  # green
    while True:
        if 5 < count <= 10:
            event.clear()  # red
            print("\33[41;1mred light is on...\033[0m")
        elif count > 10:
            event.set()  # green
            count = 0
        else:
            print("\33[42;1mgreen light is on...\033[0m")
        time.sleep(1)
        count += 1


def car(name):
    while True:
        if event.is_set():
            print("[%s] running..." % name)
            time.sleep(1)
        else:
            print("[%s] sees red light,waiting..." % name)
            event.wait()
            print("[%s] green light is on,start going..." % name)


if __name__ == '__main__':
    light = threading.Thread(target=lighter)
    light.start()

    car = threading.Thread(target=car, args=('OOOO',))
    car.start()

你可能感兴趣的:(05. 多线程之事件(Event类))