溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python多線程之事件Event的使用詳解

發布時間:2020-09-16 21:15:38 來源:腳本之家 閱讀:133 作者:上海-悠悠 欄目:開發技術

前言

小伙伴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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女