gunicorn 这个在linux 上面使用比较好 windows 上面容易报错
代码
from flask import Flask
from celery import Celery
from celery.result import AsyncResult
import time
app = Flask(__name__)
# 用以储存消息队列
app.config['CELERY_BROKER_URL'] = 'redis://127.0.0.1:6379/0'
# 用以储存处理结果
app.config['CELERY_RESULT_BACKEND'] = 'redis://127.0.0.1:6379/0'
celery_ = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery_.conf.update(app.config)
@celery_.task
def my_background_task(arg1, arg2):
# 两数相加
time.sleep(10)
return arg1 + arg2
@app.route("/sum/<arg1>/<arg2>")
def sum_(arg1, arg2):
# 发送任务到celery,并返回任务ID,后续可以根据此任务ID获取任务结果
result = my_background_task.delay(int(arg1), int(arg2))
return result.id
@app.route("/get_result/<result_id>")
def get_result(result_id):
# 根据任务ID获取任务结果
result = AsyncResult(id=result_id)
启动
[2019-07-22 14:35:47 +0800] [24475] [INFO] Starting gunicorn 19.9.0
[2019-07-22 14:35:47 +0800] [24475] [INFO] Listening at: http://0.0.0.0:7656 (24475)
[2019-07-22 14:35:47 +0800] [24475] [INFO] Using worker: sync
[2019-07-22 14:35:47 +0800] [24491] [INFO] Booting worker with pid: 24491
[root@iz2ze2vt3bzcu7n1cohvb8z test]# celery -A test.celery_ worker
/usr/local/python3/lib/python3.6/site-packages/celery/platforms.py:801: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!
Please specify a different user using the --uid option.
User information: uid=0 euid=0 gid=0 egid=0
uid=uid, euid=euid, gid=gid, egid=egid,
-------------- celery@iz2ze2vt3bzcu7n1cohvb8z v4.3.0 (rhubarb)
---- **** -----
--- * *** * -- Linux-3.10.0-693.2.2.el7.x86_64-x86_64-with-centos-7.6.1810-Core 2019-07-22 14:45:51
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: test:0x7fdaf8dc7e48
- ** ---------- .> transport: redis://127.0.0.1:6379/0
- ** ---------- .> results: redis://127.0.0.1:6379/0
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
可能会报错
kombu.exceptions.VersionMismatch: Redis transport requires redis-py versions 3.2.0 or later. You have 2.10.6
则需要更新python 的 redis 包
pip install -U redis
之后可以通过链接发送任务,并获得任务ID:
http://127.0.0.1:7656/sum/1/2
然后等待10s后通过下面这个链接可以获得任务结果:
http://127.0.0.1:7656/get_result/XXXX(任务ID)
redis里面的结构