向 Python 中的所有线程发送单向消息 [英] Send one-way message to all threads in Python

查看:30
本文介绍了向 Python 中的所有线程发送单向消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向程序中运行的每个线程发送信息,并且每个线程都必须处理该信息.

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 中的所有线程发送单向消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆