需要一个线程安全的异步消息队列

编程入门 行业动态 更新时间:2024-10-14 04:29:53
本文介绍了需要一个线程安全的异步消息队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一个Python类(最好是标准语言的一部分,而不是第3方库)来管理异步广播样式"消息.

I'm looking for a Python class (preferably part of the standard language, rather than a 3rd party library) to manage asynchronous 'broadcast style' messaging.

我将有一个线程将消息放入队列("putMessageOnQueue"方法不得阻塞),然后有多个其他线程都将在等待消息,大概调用了一些阻塞的"waitForMessage"函数.当消息放入队列时,我希望每个等待的线程都获得自己的消息副本.

I will have one thread which puts messages on the queue (the 'putMessageOnQueue' method must not block) and then multiple other threads which will all be waiting for messages, having presumably called some blocking 'waitForMessage' function. When a message is placed on the queue I want each of the waiting threads to get its own copy of the message.

我已经看过内置的Queue类,但是我认为这不合适,因为使用消息似乎涉及从队列中删除消息,因此每个消息只有一个客户端线程.

I've looked at the built-in Queue class, but I don't think this is suitable because consuming messages seems to involve removing them from the queue, so only 1 client thread would see each one.

这似乎应该是一个普通的用例,任何人都可以推荐解决方案吗?

This seems like it should be a common use-case, can anyone recommend a solution?

推荐答案

我认为典型的解决方法是为每个线程使用一个单独的消息队列,并将消息推送到先前已注册接收消息的每个队列中这样的消息.

I think the typical approach to this is to use a separate message queue for each thread, and push the message onto every queue which has previously registered an interest in receiving such messages.

像这样的事情应该起作用,但这是未经测试的代码...

Something like this ought to work, but it's untested code...

from time import sleep from threading import Thread from Queue import Queue class DispatcherThread(Thread): def __init__(self, *args, **kwargs): super(DispatcherThread, self).__init__(*args, **kwargs) self.interested_threads = [] def run(self): while 1: if some_condition: self.dispatch_message(some_message) else: sleep(0.1) def register_interest(self, thread): self.interested_threads.append(thread) def dispatch_message(self, message): for thread in self.interested_threads: thread.put_message(message) class WorkerThread(Thread): def __init__(self, *args, **kwargs): super(WorkerThread, self).__init__(*args, **kwargs) self.queue = Queue() def run(self): # Tell the dispatcher thread we want messages dispatcher_thread.register_interest(self) while 1: # Wait for next message message = self.queue.get() # Process message # ... def put_message(self, message): self.queue.put(message) dispatcher_thread = DispatcherThread() dispatcher_thread.start() worker_threads = [] for i in range(10): worker_thread = WorkerThread() worker_thread.start() worker_threads.append(worker_thread) dispatcher_thread.join()

更多推荐

需要一个线程安全的异步消息队列

本文发布于:2023-10-27 10:35:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   线程   消息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!