flask celery

from flask import Flask
from celery import Celery
from celery import platforms  #如果你不是linuxroot这两必要
platforms.C_FORCE_ROOT=True   #rootcelery


app = Flask(__name__)


def make_celery(app):
    celery = Celery('flask_celery',  # 官网使用app.import_name,因为这所有代码写在同一文件flask_celery.py,所以直接名字。
                    broker=app.config['CELERY_BROKER_URL'],
                    backend=app.config['CELERY_RESULT_BACKEND']
                    )
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery


app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379/0',
    CELERY_RESULT_BACKEND='redis://localhost:6379/1'
)
celery = make_celery(app)

@celery.task()
def long_time_def():
    for _ in range(10000):
        for j in range(10000):
            i = 1
    return 'hello'


@app.route("/")
def hello():
    print ("的任")
    result=long_time_def.apply_async()   #化在    return '的任celery'


@app.route('/t')
def test():
    return 'test!'


if __name__ == "__main__":
    app.run(debug=True, port=8080)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值