Python多執行緒之event

2021-08-02 02:02:22 字數 3821 閱讀 4162

事件(event)用於執行緒間同步和通訊。比如執行緒a要完成某一任務(event)執行緒b才能執行後面的**,怎麼實現呢,就是用event。

event常用方法

釋義set()

開始乙個事件

wait()

如果未設定set狀態會一直等待,否則過

clear()

清除set狀態

isset()

是否設定set狀態

注意:

wait方法是阻塞的

設定完set後要使用clear方法,否則wait為真,不會等待

#!/usr/bin python

#coding=utf-8

import threading

import time

defproducer

():print

u'等人來買包子....'

event.wait()

#event.clear()

print event.isset()

print

u'chef:sb is coming for baozi...'

print

u'chef:****** a baozi for sb...'

time.sleep(5)

print

u'chef:你的包子好了...'

event.set()

defconsumer

():print

u'chenchao:去買包子....'

event.set()

time.sleep(2)

print

'chenchao:waiting for baozi to be ready...'

print event.wait()

print

u'chenchao:哎呀真好吃....'

event = threading.event()

p = threading.thread(target=producer,args=())

c = threading.thread(target=consumer,args=())

p.start()

c.start()

clear()注釋掉的結果

等人來買包子....

chenchao:去買包子....

true

chef:sb is coming for baozi...

chef:****** a baozi for sb...

chenchao:waiting for baozi to be ready...

true

chenchao:哎呀真好吃....

chef:你的包子好了...

最後兩行顯然有問題,包子還沒好就可以print了,要使用clear

方法清除設定的set狀態。

#!/usr/bin python

#coding=utf-8

import threading

import time

defproducer

():print

u'等人來買包子....'

event.wait()

event.clear()

print event.isset()

print

u'chef:sb is coming for baozi...'

print

u'chef:****** a baozi for sb...'

time.sleep(5)

print

u'chef:你的包子好了...'

event.set()

defconsumer

():print

u'chenchao:去買包子....'

event.set()

time.sleep(2)

print

'chenchao:waiting for baozi to be ready...'

print event.wait()

print

u'chenchao:哎呀真好吃....'

event = threading.event()

p = threading.thread(target=producer,args=())

c = threading.thread(target=consumer,args=())

p.start()

c.start()

結果是這樣的

等人來買包子....

chenchao:去買包子....

false

chef:sb is coming for baozi...

chef:****** a baozi for sb...

chenchao:waiting for baozi to be ready...

chef:你的包子好了...

true

chenchao:哎呀真好吃....

#!/usr/bin python

#coding=utf-8

import threading

import time

defproducer

():print

u'等人來買包子....'

event.wait()

event.clear()

print event.isset()

print

u'chef:sb is coming for baozi...'

print

u'chef:****** a baozi for sb...'

time.sleep(5)

print

u'chef:你的包子好了...'

event.set()

defconsumer

():print

u'chenchao:去買包子....'

event.set()

time.sleep(2)

print

'chenchao:waiting for baozi to be ready...'

while

true:

if event.isset():

print

u'真好吃...'

break

else:

print

u'好慢啊...'

time.sleep(1)#相當於去做自己的事去了

event = threading.event()

p = threading.thread(target=producer,args=())

c = threading.thread(target=consumer,args=())

p.start()

c.start()

結果

等人來買包子....

chenchao:去買包子....

false

chef:sb is coming for baozi...

chef:****** a baozi for sb...

chenchao:waiting for baozi to be ready...

好慢啊...

好慢啊...

好慢啊...

好慢啊...chef:你的包子好了...

真好吃...

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

前言 小夥伴a,b,c圍著吃火鍋,當菜上齊了,請客的主人說 開吃!於是小夥伴一起動筷子,這種場景如何實現 event 事件 event 事件 事件處理的機制 全域性定義了乙個內建標誌flag,如果flag值為 false,那麼當程式執行 event.wait方法時就會阻塞,如果flag值為true,...

python 執行緒之同步條件(Event

usr bin python coding utf 8 author fmspider time 2018 5 3 17 16 function 同步條件 event 條件同步和條件變數同步差不多意思,只是少了鎖功能,因為條件同步設計於不訪問共享資源的 條件環境。event threading.ev...

Python程式設計 多執行緒中的event

import time,threading event threading.event 交通燈 deflighter count 0 while true if count 5 綠燈 event.set 設定標誌位 print 033 42 1m 綠燈亮 033 0m elif count 10 c...