当前位置:Gxlcms > Python > python多线程之事件Event的使用详解

python多线程之事件Event的使用详解

时间:2021-07-01 10:21:17 帮助过:28人阅读

本篇文章主要介绍了python多线程之事件Event的使用详解,现在分享给大家,也给大家做个参考。一起过来看看吧

前言

小伙伴a,b,c围着吃火锅,当菜上齐了,请客的主人说:开吃!,于是小伙伴一起动筷子,这种场景如何实现

Event(事件)

Event(事件):事件处理的机制:全局定义了一个内置标志Flag,如果Flag值为 False,那么当程序执行 event.wait方法时就会阻塞,如果Flag值为True,那么event.wait 方法时便不再阻塞。

Event其实就是一个简化版的 Condition。Event没有锁,无法使线程进入同步阻塞状态。

Event()

  1. set(): 将标志设为True,并通知所有处于等待阻塞状态的线程恢复运行状态。

  2. clear(): 将标志设为False。

  3. wait(timeout): 如果标志为True将立即返回,否则阻塞线程至等待阻塞状态,等待其他线程调用set()。

  4. isSet(): 获取内置标志状态,返回True或False。

Event案例1

场景:小伙伴a和b准备就绪,当收到通知event.set()的时候,会执行a和b线程

  1. # coding:utf-8
  2. import threading
  3. import time
  4. event = threading.Event()
  5. def chihuoguo(name):
  6. # 等待事件,进入等待阻塞状态
  7. print '%s 已经启动' % threading.currentThread().getName()
  8. print '小伙伴 %s 已经进入就餐状态!'%name
  9. time.sleep(1)
  10. event.wait()
  11. # 收到事件后进入运行状态
  12. print '%s 收到通知了.' % threading.currentThread().getName()
  13. print '小伙伴 %s 开始吃咯!'%name
  14. # 设置线程组
  15. threads = []
  16. # 创建新线程
  17. thread1 = threading.Thread(target=chihuoguo, args=("a", ))
  18. thread2 = threading.Thread(target=chihuoguo, args=("b", ))
  19. # 添加到线程组
  20. threads.append(thread1)
  21. threads.append(thread2)
  22. # 开启线程
  23. for thread in threads:
  24. thread.start()
  25. time.sleep(0.1)
  26. # 发送事件通知
  27. print '主线程通知小伙伴开吃咯!'
  28. event.set()

运行结果:

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
主线程通知小伙伴开吃咯!
Thread-1 收到通知了.
小伙伴 a 开始吃咯!
Thread-2 收到通知了.
小伙伴 b 开始吃咯!

Event案例2

场景:当小伙伴a,b,c集结完毕后,请客的人发话:开吃咯!

  1. # coding:utf-8
  2. import threading
  3. import time
  4. event = threading.Event()
  5. def chiHuoGuo(name):
  6. # 等待事件,进入等待阻塞状态
  7. print '%s 已经启动' % threading.currentThread().getName()
  8. print '小伙伴 %s 已经进入就餐状态!'%name
  9. time.sleep(1)
  10. event.wait()
  11. # 收到事件后进入运行状态
  12. print '%s 收到通知了.' % threading.currentThread().getName()
  13. print '%s 小伙伴 %s 开始吃咯!'%(time.time(), name)
  14. class myThread (threading.Thread): # 继承父类threading.Thread
  15. def __init__(self, name):
  16. '''重写threading.Thread初始化内容'''
  17. threading.Thread.__init__(self)
  18. self.people = name
  19. def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
  20. '''重写run方法'''
  21. chiHuoGuo(self.people) # 执行任务
  22. print("qq交流群:226296743")
  23. print("结束线程: %s" % threading.currentThread().getName())
  24. # 设置线程组
  25. threads = []
  26. # 创建新线程
  27. thread1 = myThread("a")
  28. thread2 = myThread("b")
  29. thread3 = myThread("c")
  30. # 添加到线程组
  31. threads.append(thread1)
  32. threads.append(thread2)
  33. threads.append(thread3)
  34. # 开启线程
  35. for thread in threads:
  36. thread.start()
  37. time.sleep(0.1)
  38. # 发送事件通知
  39. print '集合完毕,人员到齐了,开吃咯!'
  40. event.set()

运行结果:

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

1516780957.47 小伙伴 b 开始吃咯!结束线程: Thread-3

qq交流群:226296743
结束线程: Thread-2

相关推荐:

python线程池threadpool的实现

以上就是python多线程之事件Event的使用详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行