[英]Python Flask Pika Consumer (RabbitMQ)
I have two little我有两个小
Python Flask
蟒蛇烧瓶
apps应用
Both are in different docker-container
and orchestrated by docker-compose
两者都在不同的
docker-container
并由docker-compose
编排
I dont get the Data from the Producer to the Consumer...Even when I start in apptwo the start.consuming()
the Producer cant send any Data to the RabbitMQ Broker Maybe someone can help me.我不从生产者到消费者拿到的数据......甚至当我开始在apptwo的
start.consuming()
生产者不能发送任何数据到RabbitMQ的经纪人也许有人可以帮助我。 Thank you very much非常感谢
docker-compose:码头工人组成:
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 (Producer)阿彭(制作人)
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 (Consumer) 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)
Goal In this easy example I just want to receice the data in apptwo and store it in the data list...目标在这个简单的例子中,我只想接收 apptwo 中的数据并将其存储在数据列表中......
Thanks again!!再次感谢!!
In apptwo (Consumer):在 apptwo(消费者)中:
thread = Thread(channel.start_consuming())
thread.start()
Here the constructor call of Thread is never called, since channel.start_consuming is called before, which is blocking.这里从不调用 Thread 的构造函数调用,因为之前调用了 channel.start_sumption,这是阻塞的。 Changing your code to the following might help.
将您的代码更改为以下内容可能会有所帮助。
thread = Thread(target = channel.start_consuming)
thread.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.