我需要向程序中运行的每个线程发送信息,并且每个线程都必须处理该信息.
I need to send information to every thread that's running in my program, and every thread has to process that information.
我不能使用常规队列来做到这一点,因为一旦一个线程从队列中删除数据,所有其他线程将无法再看到它.
I can't do it using a regular queue, because that way once one thread removes the data from the queue all the other threads won't be able to see it anymore.
实现这一目标的最佳方法是什么?
What's the best way of achieving this?
一种方法是为每个线程设置一个队列,广播信息的函数负责将消息插入到每个线程的队列.
One way is to have a queue for each thread, and the function that broadcasts the information is responsible for inserting the message into the queue of every thread.
例如,这类似于消息队列在 Windows 中的工作方式.每个执行 GUI 操作的线程都有一个关联的消息队列,独立于任何其他线程.
This is similar to the way message queues work in Windows, for example. Each thread that does GUI operations has an associated message queue, independent from that of any other thread.
这篇关于向 Python 中的所有线程发送单向消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!