gpt4 book ai didi

Python Flask Pika Consumer (RabbitMQ)

转载 作者:行者123 更新时间:2023-12-04 02:44:58 25 4
gpt4 key购买 nike

我有两个小

Python Flask



应用
  • Appone --> 制作人
  • Apptwo --> 消费者

  • 两者都不一样 docker-containerdocker-compose 编排

    我没有从生产者到消费者的数据......即使我在 apptwo 中开始 start.consuming() Producer 无法向 RabbitMQ Broker 发送任何数据
    也许有人可以帮助我。非常感谢

    docker 组成:
    version: '3'
    services:

    appone:
    container_name: appone
    restart: always
    build:
    context: ./appone
    dockerfile: Dockerfile
    environment:
    FLASK_APP: ./app.py
    volumes:
    - './appone:/code/:cached'
    ports:
    - "5001:5001"

    apptwo:
    container_name: apptwo
    restart: always
    build:
    context: ./apptwo
    dockerfile: Dockerfile
    environment:
    FLASK_DEBUG: 1
    FLASK_APP: ./app.py
    volumes:
    - ./apptwo:/code:cached
    ports:
    - "5002:5002"

    rabbitmq:
    image: "rabbitmq:3-management"
    hostname: "rabbit"
    ports:
    - "15672:15672"
    - "5672:5672"
    labels:
    NAME: "rabbitmq"
    volumes:
    - ./rabbitmq/rabbitmq-isolated.conf:/etc/rabbitmq/rabbitmq.config

    appone(制作人)
    from flask import Flask
    from flask_restful import Resource, Api
    import pika

    app = Flask(__name__)
    api = Api(app)

    app.config['DEBUG'] = True

    message = "Hello World, its me appone"


    class HelloWorld(Resource):
    def get(self):
    connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='rabbitmq'))
    channel = connection.channel()

    channel.queue_declare(queue='hello', durable=True)

    channel.basic_publish(exchange='', routing_key='hello', body='Hello World!', properties=pika.BasicProperties(delivery_mode=2))

    connection.close()

    return {'message': message}


    api.add_resource(HelloWorld, '/api/appone/post')

    if __name__ == '__main__':
    # Development
    app.run(host="0.0.0.0", port=5001)

    apptwo(消费者)
    from flask import Flask
    from flask_restful import Resource, Api
    import pika
    from threading import Thread

    app = Flask(__name__)
    api = Api(app)

    app.config['DEBUG'] = True

    data = []

    connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='rabbitmq'))

    channel = connection.channel()

    channel.queue_declare(queue='hello', durable=True)

    def callback(ch, method, properties, body):
    data.append(body)
    ch.basic_ack(delivery_tag = method.delivery_tag)

    channel.basic_consume(queue='hello', on_message_callback=callback)

    thread = Thread(channel.start_consuming())
    thread.start()

    class HelloWorld(Resource):
    def get(self):
    return {'message': data}

    api.add_resource(HelloWorld, '/api/apptwo/get')

    if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=5002)

    目标
    在这个简单的例子中,我只想接收 apptwo 中的数据并将其存储在数据列表中......

    再次感谢!!

    最佳答案

    在 apptwo(消费者)中:

    thread = Thread(channel.start_consuming())
    thread.start()
    这里从不调用 Thread 的构造函数调用,因为之前调用了 channel.start_sumption,这是阻塞的。将您的代码更改为以下内容可能会有所帮助。
    thread = Thread(target = channel.start_consuming)
    thread.start()

    关于Python Flask Pika Consumer (RabbitMQ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578645/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com