如何在线程中使用 python 中的事件对象?

How to use Event objects in python with a thread?

我有一个主 python 脚本,它在后台启动一个线程 运行。

poll = threading.Thread(target=poll_files, args=myargs)

我希望我的主脚本等到我的 poll 线程中发生特定的事情。我想使用一个事件对象。所以在我的主脚本中,我这样做:

trigger = threading.Event()

当我想等待时:

trigger.wait()

我的问题是,在我的 poll 线程中,如何将事件设置为 True?我知道我知道:

trigger.set()

但是我的 poll 线程中是否还需要 trigger = threading.Event()

Event 对象传递给线程目标函数,以便它们在主线程和 pool 线程之间共享:

def poll_files(....., trigger):
    ....
    trigger.set()

# main thread
trigger = threading.Event()
poll = threading.Thread(target=poll_files, args=myargs + (trigger,))
...
trigger.wait()