1

项目结构

.
+-- core                    # 逻辑操作
|   +-- __init__.py
+-- www                     
|   +-- templates           # 模板 
|   +-- views               # 视图
|   |   +-- task.py           
|   |   +-- __init__.py
|   +-- model.py            # db 模型
|   +-- __init__.py     
+-- application.py          # 程序入口
+-- config.py               # 配置文件
+-- celery_worker.py        # celery worker

1. 依赖安装

pip3 install celery eventlet redis flask flask_restx flask_cors

2. 初始化

# /www/__init__.py

from flask import Flask, render_template
from flask_cors import CORS
from celery import Celery
from config import Config

celery = Celery(__name__, broker="redis://username:password@localhost:6379/0")

def init_celery(app, celery):
    """Add flask app context to celery.Task"""
    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
    celery.conf.update(app.config)

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    from .views import api
    api.init_app(app)
    celery.conf.update(app.config)
    CORS(app, supports_credentials=True)
    
    return app

3. 编写 Task

# /www/views/task.py

@celery.task()
def long_time_task(config):
    return func(config)

4. 定义视图

# /www/views/task.py

@api.route('/task')
class TaskApi(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
     
    def put(self):
        config = request.get_json()
        task = long_time_task.apply_async(args=[config])
        
    def get(self):
        task_id = request.args.get('task_id', None)
        task = long_time_task.AsyncResult(task_id)
        return {
            'state': task.state,
            'current': 0,
            'total': 1,
            'result': task.result
        }

5. 定义 Celery Worker

# celery_worker.py

from www import celery, create_app

app = create_app()
app.app_context().push()

6. 启动 Flask App & Celery

#!/usr/bin/env bash

python3 application.py & celery worker -A celery_worker.celery --loglevel=info --pool=eventlet

参考

https://github.com/miguelgrin...

https://github.com/miguelgrin...


jiyang
12 声望8 粉丝

Everything will be happy endding : )