同时从Rabbitmq接收日志并运行烧瓶应用程序

问题描述 投票:7回答:1

我已经安装了rabbitmq并且可以正常工作,并且我知道如何接收日志,但是不知道如何使用flask向UI显示它。

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'

我不知道如何使用多线程来运行flask应用程序并不断从队列接收日志。

python multithreading flask rabbitmq rabbitmqctl
1个回答
0
投票

您的flask应用程序(这里是主线程)运行一段时间或由uwsgi或您用于运行它的任何其他程序确定的请求数量。当主进程停止时,很可能是错误地时候正常关闭amqp连接。

此外,您的应用程序实例可能同时运行多个(请考虑uwsgi processes,因此您将在每个工作程序/进程上获得一些日志。

明智的方法是将这两件事分开。在Web应用程序范围之外为日志运行使用者进程,即:使用超级用户。

© www.soinside.com 2019 - 2024. All rights reserved.