您现在的位置是:首页 > 电脑 > 

python threading.Event:添加事件参数(python threading.Event: add event params)

2025-07-20 11:51:03
python threading.Event:添加事件参数(python threading.Event: add event params) 我是否可以使用来自线程模块的Event对象,不仅可以通知某些事件已发生,还可以提供此事件的一些参数,例如: e = Event() ... e.param = "this is event data" e.set()
python threading.Event:添加事件参数(python threading.Event: add event params)

我是否可以使用来自线程模块的Event对象,不仅可以通知某些事件已发生,还可以提供此事件的一些参数,例如:

e = Event() ... e.param = "this is event data" e.set()

另一个线程:

e.wait() data = e.param

乍一看似乎没问题,但是有什么问题可以发生吗? 安全吗? 如果没有,还有什么方法可以更好地在线程之间传递一些事件参数?

感谢名单。

Can I use Event object from threading module not only to notify that some event has happened but also to give some params of this event, for example:

e = Event() ... e.param = "this is event data" e.set()

Another thread:

e.wait() data = e.param

It seems to be ok at first glance, but are there any problems than can happen? Is it safe? If not, what other way is better to pass some events params between threads?

Thanx.

最满意答案

您实际上不需要将值附加到Event对象,您可以使用与Event分开的其他全局,属性等,并使用Event来表示它已更新。 这是通常的做事方式。

但是你所做的事情真的没什么不对 。 除了使用信号事件的常规竞争问题之外,它不会增加任何其他问题。 然而,它似乎有点误导 - 它使得看起来好像param以某种方式同步,而不是。

如果您尝试发信号通知新值已准备好,并且同步对该值的访问,则几乎总是需要Condition ,如下所示:

c = Condition() data = one ... with c: data = "new data" () ... with c: while data is one: c.wait()

或者,更简单地说,只是使用queue而不是首先共享变量:

q = Queue() ... q.put(data) ... data = q.get()

You don't really need to attach the value to the Event object, you can just use some other global, attribute, etc. separate from the Event, and use the Event to signal that it's been updated. And that's the usual way of doing things.

But there's really nothing wrong with what you're doing. And it doesn't add any other problems beyond the usual race problems with using events for signaling. However, it does seem a bit misleading—it makes it seem as if the param is somehow synchronized, when it isn't.

If you're trying to signal that a new value is ready, and synchronize access to that value you almost always want a Condition, like this:

c = Condition() data = one ... with c: data = "new data" () ... with c: while data is one: c.wait()

Or, more simply, just use a queue and don't share a variable in the first place:

q = Queue() ... q.put(data) ... data = q.get()

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/79479.html

相关标签:无
上传时间: 2023-04-28 04:54:03
留言与评论(共有 9 条评论)
本站网友 整形隆胸
29分钟前 发表
然而
本站网友 路虎4s店
29分钟前 发表
Can I use Event object from threading module not only to notify that some event has happened but also to give some params of this event
本站网友 全运会志愿者
11分钟前 发表
and synchronize access to that value you almost always want a Condition
本站网友 注意事项
12分钟前 发表
attribute
本站网友 rcse
19分钟前 发表
what other way is better to pass some events params between threads? Thanx. 最满意答案 您实际上不需要将值附加到Event对象
本站网友 挖金矿工
26分钟前 发表
不仅可以通知某些事件已发生
本站网友 健康晚餐食谱
18分钟前 发表
just use a queue and don't share a variable in the first place
本站网友 玻璃钢格栅厂家
27分钟前 发表
但是你所做的事情真的没什么不对