簡體   English   中英

在python線程之間使用事件

[英]use of events between python threads

我開始學習編寫多線程python代碼。 特別是我正在嘗試在線程之間使用事件。 由於某些原因,下面的代碼無法正常工作,我找不到原因。 你有什么建議嗎? 先感謝您!

import threading
import time

e1 = threading.Event()

def counting_thread():
    x=0
    while 1:
        print(x)
        if x==5:
            e1.set
        x=x+1
        if x==11:
            x=0
        time.sleep(1)

def speaking_thread():
    while not e1.wait():
        print('You just said five!')

t1 = threading.Thread(target=counting_thread)
t1.start()

t2 = threading.Thread(target=speaking_thread)
t2.start()

您實際上沒有調用set方法。

    if x==5:
        e1.set()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM