使用 Quart 运行 RabbitMQ Pika [英] Running RabbitMQ Pika with Quart

查看:74
本文介绍了使用 Quart 运行 RabbitMQ Pika的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Quart 框架,但我也需要使用 RabbitMQ Pika 连接器,但我无法让它们发挥良好的作用,因为它们都有无限循环.

I am using the Quart framework, but I also need to use the RabbitMQ Pika connector, but I can't get them to play nice as they both have infinite loops.

入口点:

from quart import Quart
from .service import Service

app = Quart(__name__)

@app.before_serving
async def startup():
app.service_task = asyncio.ensure_future(service.start())

if not service.initialise():
    sys.exit()

服务类:

class Service:
def __init__(self, new_instance):
    self._connection = None
    self._channel = None
    self._messaging_thread = None

def initialise(self):

    credentials = pika.PlainCredentials('username', 'password')
    parameters = pika.ConnectionParameters('localhost', credentials=credentials)
    self._connection = pika.BlockingConnection(parameters)
    self._channel = self._connection.channel()
    self._channel.queue_declare(queue='to_be_processed_queue')

    self._channel.basic_consume(queue='to_be_processed_queue',
                                auto_ack=True,
                                on_message_callback=self.callback)
  
    print('creating thread')
    self._messaging_thread = Thread(target=self.run_consume())
    #self._messaging_thread.start()
    print('Thread created...')

def run_consume(self):
    try:
        self._channel.start_consuming()
    except KeyboardInterrupt:
        self._shutdown()

代码甚至没有进入print('Thread created...'),我不明白.从这个问题我知道RabbitMQ不是线程安全的,但我不知道不明白如何运行 RabbitMQ.

The code isn't even getting to the print('Thread created...') and I don't understand. From this question I do understand that RabbitMQ isn't thread-safe, but I don't understand how else to run RabbitMQ.

推荐答案

Pika 不是线程安全的,正如您已经发现的,但这不是您的程序阻塞的原因.

Pika is not thread safe as you have already spotted but this is not why your program blocks.

您的问题可能出在这里:

Your problem might be here:

print('creating thread')
self._messaging_thread = Thread(target=self.run_consume())
#self._messaging_thread.start()

如果去掉 run_consume 中的括号,效果会更好吗?现在您实际上不是在创建线程而是在现场执行 self.run_consume() 并且它不会退出.

Does it work better if you remove parentheses from run_consume? Now you are actually not creating a thread but executing self.run_consume() on the spot and it does not exit.

self._messaging_thread = Thread(target=self.run_consume)

将是我的第一次尝试.

但是,由于 Pika 不是线程安全的,因此您还必须移动您的频道创建 &东西到你的线程而不是在主程序中做.如果您不在其他任何地方使用它,它可能会起作用,但使用 Pika 的正确方法是将所有内容完全包含在线程中,而不是像您现在在这里那样在线程之间共享任何 Pika 结构.

However, as Pika is not thread safe, you must also move your channel creation & stuff to your thread instead of doing that in the main program. It might work if you are not using it anywhere else but the correct way with Pika is to contain absolutely everything in the thread and not share any Pika structures between threads as you do now here.

这篇关于使用 Quart 运行 RabbitMQ Pika的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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