使用“gevent.event.Event”进行多监听器通知的更好模式?
我发现大多数时候当我使用 gevent.event.Event 时,我的代码看起来像这样:
old_event = self.some_event
self.some_event = Event()
old_event.set()
监听器看起来像这样:
while 1:
self.some_event.wait()
… do stuff …
这是“正确的方法吗”?或者是否有更好的方法来通知多个侦听器重复发生的事件?
I've found that most of the time when I use gevent.event.Event
, my code looks something like this:
old_event = self.some_event
self.some_event = Event()
old_event.set()
With the listeners looking something like:
while 1:
self.some_event.wait()
… do stuff …
Is this the “right way to do it”? Or is there a better way to notify multiple listeners of a recurring event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您还可以
clear()
该事件。这将通知当前正在等待该事件的侦听器,但稍后启动 wait() 的侦听器将被阻塞,直到下一次调用
set()
。Well, you can also
clear()
the event.This will notify the listeners that are currently waiting for the event, but the listeners that are started to wait() later will be blocked until the next call to
set()
.