首頁  >  文章  >  後端開發  >  python多執行緒之事件Event的使用詳解

python多執行緒之事件Event的使用詳解

不言
不言原創
2018-04-27 09:59:242578瀏覽

本篇文章主要介紹了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執行緒

# 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 開始吃咯!

Event案例2

場景:當小夥伴a,b,c集結完畢後,請客的人發話:開吃咯!


# 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 小伙伴 %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("qq交流群:226296743")
    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-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的使用詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn