在Flask vi中监听RabbitMQ队列

2024-05-29 03:10:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个视图,其中我向工人发送一条消息(请求id)。然后,我需要监听结果队列,只在收到响应消息时返回响应(响应必须包含此消息)。如何正确地执行?在

这是我的代码:

def get(self):
    ruid = rand_ruid()
    # add msg to q1
    write_ch = current_app.amqp_conn.channel()
    write_ch.queue_declare(queue='q1', durable=True)
    msg = mkmsg(ruid=ruid)
    write_ch.basic_publish(exchange='', routing_key='q1', body=msg)
    write_ch.close()

    # then wait results from qbus
    listen_ch = current_app.amqp_conn.channel()
    listen_ch.exchange_declare(exchange='exbus', type='direct')
    listen_ch.queue_declare(queue='bus')

    listen_ch.queue_bind(exchange='exbus', queue='bus', routing_key=ruid)
    while 1:
        for method, properties, body in listen_ch.consume('bus'):
            if method and body:
                listen_ch.basis_ack(delivery_tag=method.delivery_tag)
                listen_ch.close()
                return make_response(body)

更新 我的问题是不正确的,因为我的方法。我想在程序的同步部分(flask view)执行异步操作(等待来自队列的结果)。在


Tags: 消息exchange队列queuebodymsgcurrentch

热门问题