simultaneously receive logs from Rabbitmq and run your flask app
Asked Answered
P

1

7

I have rabbitmq installed and working properly and I know how to receive logs but don't know how to show it to UI with flask.

flask_app.py

from flask import Flask
from threading import Thread
app = Flask(__name__)
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs',
                     type='fanout')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

channel.queue_bind(exchange='logs',
               queue=queue_name)

print('[*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
    print(body)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

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

@app.route('/')
def index():
    return 'hi'

I don't know how to use multi threading to run flask app and continuously receive logs from queue.

Poultice answered 21/7, 2017 at 11:43 Comment(0)
P
1

Your flask app, here the main thread, runs for an amount of time or an amount of requests determined by your uwsgi or whatever else you are using to run it. When the main process stops, it will most likely be the wrong time to gracefully close your amqp connection.

Moreover, there could be more than one instance of your application running at the same time (think uwsgi processes), so you would get bits of logs on each worker/process.

The sane approach here is to keep these two things separate. Run a consumer process for your logs outside the scope of your web application, ie: with supervisord.

Perambulate answered 7/10, 2019 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.