简体   繁体   English

在python线程之间使用事件

[英]use of events between python threads

I'm starting to learn writing multithreaded python code. 我开始学习编写多线程python代码。 In particular I'm trying to use events between thread. 特别是我正在尝试在线程之间使用事件。 For some reason the code below is not working and I can't find out why. 由于某些原因,下面的代码无法正常工作,我找不到原因。 Do you have any suggestion? 你有什么建议吗? Thank you in advance! 先感谢您!

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()

You didn't actually call the set method. 您实际上没有调用set方法。

    if x==5:
        e1.set()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM