Send one-way message to all threads in Python(向 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 中的所有线程发送单向消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 Python 中的所有线程发送单向消息
基础教程推荐
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
