flask扩展 flask-celery

2022-09-14 18:55:52 浏览数 (1)

一、flask-celery安装

pip install celery

pip install redis==2.10.6

pip install flask-celery-helper

二、创建和加载

工程目录下创建run_celery.py文件

代码语言:javascript复制
<span class="hljs-comment">#定义创建celery对象的方法</span>
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> Celery

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_celery</span><span class="hljs-params">(app)</span>:</span>
    celery = Celery(
        app.import_name,
        backend=app.config[<span class="hljs-string">'CELERY_RESULT_BACKEND'</span>],
        broker=app.config[<span class="hljs-string">'CELERY_BROKER_URL'</span>]
    )
    celery.conf.update(app.config)

    TaskBase = celery.Task
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ContextTask</span><span class="hljs-params">(celery.Task)</span>:</span>
        abstract = <span class="hljs-keyword">True</span>
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self, *args, **kwargs)</span>:</span>
            <span class="hljs-keyword">with</span> app.app_context():
                <span class="hljs-keyword">return</span> TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    <span class="hljs-keyword">return</span> celery

manage.py

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask_script <span class="hljs-keyword">import</span> Manager
<span class="hljs-keyword">from</span> flask_migrate <span class="hljs-keyword">import</span> Migrate, MigrateCommand

<span class="hljs-keyword">from</span> app <span class="hljs-keyword">import</span> create_app
<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> db

<span class="hljs-keyword">from</span> run_celery <span class="hljs-keyword">import</span> make_celery

<span class="hljs-comment"># 将要生成表的模型类导入</span>
<span class="hljs-keyword">from</span> myApp.models <span class="hljs-keyword">import</span> *

app = create_app(__name__)
celery = make_celery(app)

<span class="hljs-comment">#创建迁移管理对象</span>
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command(<span class="hljs-string">"db"</span>, MigrateCommand)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
    manager.run()

exts/ext_celery

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask_celery <span class="hljs-keyword">import</span> Celery
celery = Celery()
<span class="hljs-keyword">from</span> .ext_celery <span class="hljs-keyword">import</span> celery

关联manage.py中的celery与ext_celery中的cel

app.py

代码语言:javascript复制
<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> celery
celery.init_app(app)

三、配置

代码语言:javascript复制
<span class="hljs-comment"># celery</span>
CELERY_RESULT_BACKEND = <span class="hljs-string">"redis://:@127.0.0.1:6379/5"</span>
CELERY_BROKER_URL = <span class="hljs-string">"redis://:@127.0.0.1:6379/6"</span>
<span class="hljs-comment"># celery的工人一直启动可能会造成内存泄露,该参数规定每个工人执行了多少个任务后就会被杀死</span>
<span class="hljs-comment"># CELERY_MAX_TASKS_PER_CHILD = 50</span>

四、封装任务

在应用目录下创建名为tasks的包目录

代码语言:javascript复制
<span class="hljs-comment"># from manage import celery</span>
<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> celery

<span class="hljs-meta">@celery.task()</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mail</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"----开始耗时操作----"</span>)
    <span class="hljs-keyword">import</span> time
    time.sleep(<span class="hljs-number">5</span>)
    print(<span class="hljs-string">"----结束耗时操作----"</span>)

五、添加到队列

代码语言:javascript复制
<span class="hljs-keyword">from</span> myApp.tasks <span class="hljs-keyword">import</span> mail
<span class="hljs-meta">@myApp.route("/sendMail/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sendMail</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment">#将耗时任务添加到队列</span>
    mail.delay()
    <span class="hljs-keyword">return</span> <span class="hljs-string">"邮件发送成功"</span>

六、启动flask服务

python manage.py runserver -d -r

七、启动工人

代码语言:javascript复制
celery worker -A manage.celery --loglevel=info

0 人点赞