【问题标题】:Is there better way to process messages received in a queue from the on_message callback?有没有更好的方法来处理从 on_message 回调队列中收到的消息?
【发布时间】:2020-03-02 10:06:55
【问题描述】:

我目前有一个带有消息队列和 on_message 回调函数的 paho-MQTT 客户端脚本。

    def on_message(self, client, _, message):
        message = message.payload.decode()
        self.messageQueue.put_nowait(message)


我需要对这些收到的消息进行一些处理(我每 3 秒在队列中收到大约 12 条消息)

我怎样才能安全地处理这些消息?

【问题讨论】:

    标签: python multithreading queue paho concurrent.futures


    【解决方案1】:

    如果您想要一个健壮的分布式队列机制,您可以使用 python celery 实现,您的 on_message 将充当将任务放置在代理上的生产者。并且多个工人/消费者将在不同的机器上运行并从代理消费任务。

    链接:http://docs.celeryproject.org/en/latest/getting-started/introduction.html

    希望这会有所帮助!

    【讨论】:

    • 有没有办法在没有外部库的情况下实现这一点并最大限度地减少开销?
    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2014-03-07
    相关资源
    最近更新 更多