flask + celery

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里面的结构 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值