本文主要是介绍flask + celery + redis + flower,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
flask:
pip3 install flask celery
flask 项目文件:
包含 app.py,config.py。
app.py:
from flask import Flask, flash, render_template, request, redirect, url_for
from celery import Celeryapp = Flask(__name__)
app.config.from_object("config")
app.secret_key = app.config['SECRET_KEY']client = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
client.conf.update(app.config)@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'GET':send_mail.apply_async(args=['data'], countdown=5)return 'get ' + app.secret_keyelif request.method == 'POST':# flash(“Message scheduled”)send_mail.apply_async(args=['data'], countdown=5)return 'post'@client.task
def send_mail(data):""" Function to send emails."""with app.app_context():msg = {}msg_body = dataprint(msg_body)return msg_bodyif __name__ == '__main__':app.run(host='0.0.0.0',port=8000,debug=True)
config.py:
CELERY_BROKER_URL = 'redis://localhost:6379/0'
SECRET_KEY = 'sk1'
celery:
启动:celery -A app.client worker --loglevel=info --autoscale=5,1
redis:
启动:docker run -d -p 6379:6379 redis
flower:
pip3 install flower==1.2.0
启动:celery --broker=redis://localhost:6379/0 flower --port=8080
这篇关于flask + celery + redis + flower的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!