python - Python线程 - 如何等待来自多个发件人的数据合并结果?
问题描述
我的 ImageStitcher 类正在接收来自不同线程的多个图像消息。然后另一个线程将调用 get_stichted_image() 并且这有效。但它看起来并不好,似乎有点慢。
有没有更好的方法来处理来自不同线程的多个传入消息并等待所有队列(或其他东西)包含一些东西?
class ImageStitcher:
def __init__(foo):
self.image_storage = {
Position.top: queue.Queue(maxsize=1),
Position.bottom: queue.Queue(maxsize=1)
}
foo.register(image_callback)
# will be called from different threads
def image_callback(self, image_msg):
if self.image_storage[image_msg["position"]].full():
self.image_storage[image_msg["position"].get()
self.image_storage[image_msg["position"].put(image_msg)
def get_stichted_image(self):
try:
# the following code is ugly and seems to be slow
top_image_msg = self.image_storage[Position.top].get(timeout=0.1)
bottom_image_msg = self.image_storage[Position.bottom].get(timeout=0.1)
return self.stitch_images(top_image_msg, bottom_image_msg)
except queue.Empty:
return None
解决方案
推荐阅读
- laravel - 如何修复 Laravel 中的错误 npm “这与 npm 无法找到文件有关”?
- python - Pandas 从二维数组的字典中创建数据框
- javascript - 是否需要用“min”命名最小化文件?
- functional-programming - Java Lambda 函数接口提取到常用方法
- twitter - Headless Chrome 不执行 JS
- python - Python中的排序函数未返回预期结果
- windows - 为什么某些 WCF 命名管道客户端会收到 TimeoutException
- node.js - Discord.js - 如何让机器人通过服务器 ID 打印服务器名称?
- javascript - Ionic 4 Deeplink 插件返回错误路由不匹配
- mysql - MySQL 服务器版本,用于在第 3 行的 '' 附近使用正确的语法