Python 线程。事件线程安全吗?

Python 线程。事件线程安全吗?,python,multithreading,Python,Multithreading,线程。事件线程安全吗?通过调用Class.event.wait(),我可以安全地访问事件吗 示例代码 import threading import time class Worker(threading.Thread): def __init__(self): super(Worker, self).__init__() self.startup_event = threading.Event() def run(self):

线程。事件线程安全吗?通过调用
Class.event.wait()
,我可以安全地访问事件吗

示例代码

import threading
import time

class Worker(threading.Thread):
    def __init__(self):
        super(Worker, self).__init__()
        self.startup_event = threading.Event()

    def run(self):
        time.sleep(4)
        print('Hello World')
        self.startup_event.set()


w = Worker()
w.start()
w.startup_event.wait()
print('%s started' % w.__class__.__name__)

是的。它是一个用于此目的的同步原语。请看一些例子。@mfukar:记住,我们谈论的是一种曾经包含在其标准库中的语言。但是你是对的
事件
对象应该与线程一起使用。