centos 7安装redis
yum install redis
python安装redis模块
pip install redis
安装celery
pip install celery
test.py
#encoding:utf-8
from flask import Flask
from celery import Celery
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
celery = Celery('test', broker=app.config['CELERY_BROKER_URL'],backend=app.config['CELERY_RESULT_BACKEND'])
celery.conf.update(app.config)
@celery.task()
def test(a, b):
return str(a+b)
@app.route('/test')
def test():
task = test.delay(10, 20)
return str(task)
if __name__ == '__main__':
app.run(debug=True,host='127.0.0.1',port=5000)
运行
python test.py
在test.py同级目录运行celery -A test.celery worker -l info
访问http://127.0.0.1:5000/test
文章评论