繁体   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