Patterns¶
Flask-Coney can be used to archive different messaging patterns. A few typical patterns, found on RabbitMQ Tutorials, will be displayed here.
Hallo World¶
In this example we have two services (aka flask applications). Service 1 will receive data from a http post request. It will send this data to the message broker with the routing_key “process”. Service 2 will pick up any message with the routing key “process”, process and store the data. Service 2 also provides an api endpoint, which allows a user to request the processed data.
Service 1¶
from flask import Flask, request
from flask_coney import Coney
app = Flask(__name__)
app.config["CONEY_BROKER_URI"] = "amqp://guest:guest@rabbitmq"
coney = Coney(app)
@app.route("/process", methods=["POST"])
def process():
data = request.get_json()
# validation ...
coney.publish(data, routing_key="process")
return "will be processed, check service2"
Serivce 2¶
from flask import Flask
from flask_coney import Coney
app = Flask(__name__)
app.config["CONEY_BROKER_URI"] = "amqp://guest:guest@rabbitmq"
coney = Coney(app)
@coney.queue(queue_name="process")
def process_queue(ch, method, props, body):
# do something with body
processed_body = body
print(processed_body, flush=True)