前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 多线程 事件event(并行编程 7)

python 多线程 事件event(并行编程 7)

作者头像
用户5760343
发布2019-07-30 11:03:42
4340
发布2019-07-30 11:03:42
举报
文章被收录于专栏:sktjsktj

a.wait() b.wait() c.wait() /// set() //all start()

coding:utf-8

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 开始吃咯!'%name

设置线程组

threads = []

创建新线程

thread1 = threading.Thread(target=chihuoguo, args=("a", )) thread2 = threading.Thread(target=chihuoguo, args=("b", ))

添加到线程组

threads.append(thread1) threads.append(thread2)

开启线程

for thread in threads: thread.start()

time.sleep(0.1)

发送事件通知

print '主线程通知小伙伴开吃咯!' event.set()

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

threading.Event()

ev.wait() ev.set() ev.clear()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.07.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • a.wait() b.wait() c.wait() /// set() //all start()
  • coding:utf-8
  • 设置线程组
  • 创建新线程
  • 添加到线程组
  • 开启线程
  • 发送事件通知
  • threading.Event()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档