Python even 线程通讯

原文链接: Python even 线程通讯

上一篇: Python numpy 自定义函数和下标选取

下一篇: tf 自编码 矩阵信息提取

Event对象

用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象

event对象默认为假(Flase),即遇到event对象在等待就阻塞线程的执行。

3f31379d3a4bdeb3e853945289a643e4f11.jpg

全局通知,类似发布订阅模型

import threading
import time

event = threading.Event()


def chiHuoGuo(name):
    # 等待事件,进入等待阻塞状态
    print('%s 已经启动' % threading.currentThread().getName())
    print('小伙伴 %s 已经进入就餐状态!' % name)
    time.sleep(1)
    event.wait()
    # 收到事件后进入运行状态
    print('%s 收到通知了.' % threading.currentThread().getName())
    print('%s 小伙伴 %s 开始吃咯!' % (time.time(), name))


class myThread(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, name):
        '''重写threading.Thread初始化内容'''
        threading.Thread.__init__(self)

        self.people = name

    def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        '''重写run方法'''

        chiHuoGuo(self.people)  # 执行任务
        print("结束线程: %s" % threading.currentThread().getName())


# 设置线程组
threads = []
# 创建新线程
thread1 = myThread("a")
thread2 = myThread("b")
thread3 = myThread("c")

# 添加到线程组
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)

# 开启线程
for thread in threads:
    thread.start()

time.sleep(0.1)
# 发送事件通知
print('集合完毕,人员到齐了,开吃咯!')
event.set()

结果如下,注意线程接受事件的顺序未必是按照其启动顺序的

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
Thread-3 已经启动
小伙伴 c 已经进入就餐状态!
集合完毕,人员到齐了,开吃咯!
Thread-2 收到通知了.
Thread-3 收到通知了.
Thread-1 收到通知了.
1534917791.6462271 小伙伴 a 开始吃咯!
结束线程: Thread-1
1534917791.6462271 小伙伴 c 开始吃咯!
结束线程: Thread-3
1534917791.6462271 小伙伴 b 开始吃咯!
结束线程: Thread-2

相互传递信息的类型,使用两个event对象

import queue
from random import randint
from threading import Thread
from threading import Event


class WriteThread(Thread):
    def __init__(self, q, WE, RE):
        super().__init__()
        self.queue = q
        self.RE = RE
        self.WE = WE

    def run(self):
        data = [randint(1, 10) for _ in range(0, 5)]
        self.queue.put(data)
        print("WriteThread 写队列:", data)
        self.RE.set()  # 通知读线程可以读了
        print("WriteThread 通知读事件")
        print("WriteThread 等待写事件")
        self.WE.wait()  # 等待写事件
        print("WriteThread 收到写事件")
        # self.WE.clear()  # 清除写事件,以方便下次读取


class ReadThread(Thread):
    def __init__(self, q, WE, RE):
        super().__init__()
        self.queue = q
        self.RE = RE
        self.WE = WE

    def run(self):
        while True:
            self.RE.wait()  # 等待读事件
            print("ReadThread 收到读事件")
            data = self.queue.get()
            print("ReadThread 读队列 {0}".format(data))
            print("ReadThread 发送写事件")
            self.WE.set()  # 发送写事件
            self.RE.clear()  # 清除读事件,以方便下次读取


q = queue.Queue()
WE = Event()
RE = Event()
writethread = WriteThread(q, WE, RE)
readthread = ReadThread(q, WE, RE)

writethread.start()
readthread.start()

a = input()
print(a)

RE,WE,分别表示读取和写入的event对象

WriteThread 写队列: [8, 8, 9, 7, 6]
WriteThread 通知读事件
WriteThread 等待写事件
ReadThread 收到读事件
ReadThread 读队列 [8, 8, 9, 7, 6]
ReadThread 发送写事件
WriteThread 收到写事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值