简体   繁体   English

运行 Celery 任务锁定 Flask

[英]Run Celery task locking Flask

I am trying to organize project structure for Flask+Celery app.我正在尝试为 Flask+Celery 应用程序组织项目结构。 When i worked in single file all works fine.当我在单个文件中工作时,一切正常。 But when i distributed code in modules, calling test_task.apply_async() is locking flask.但是当我在模块中分发代码时,调用 test_task.apply_async() 会锁定 flask。 My project structure:我的项目结构:

web_spider/
 app/
  __init__.py
  rest/
   __init__.py
   views/
    __init__.py
    test_view.py
   flask_app.py
  task_runner/
   __init__.py
   celery_app.py
   tasks.py
 requirements.txt

test_view.py test_view.py

import flask
from app.task_runner.tasks import test_task

api_test_view = flask.Blueprint('api_test_view', __name__)

@api_test_view.route('/')
def test_view():
 test_task.apply_async() #lock there
 return 'Hello, World!'

flask_app.py烧瓶应用程序.py

import flask
from app.rest.views.api_test_view import test_view

flask_app = flask.Flask(__name__)
flask_app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
flask_app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
flask_app.register_blueprint(test_view)

if __name__ == '__main__':
flask_app.run(debug=True)

celery_app.py celery_app.py

from app.rest.flask_app import flask_app
import celery

celery_app = celery.Celery(flask_app.name, broker=flask_app.config['CELERY_BROKER_URL'])

celery_app.conf.update(flask_app.config)

tasks.py任务.py

from celery import shared_task

@shared_task
def test_task():
 return 1 + 1

Probably, you don't load celery_app and shared_task can't find the Celery app to work with.很可能,您没有加载celery_app并且 shared_task 找不到可以使用的 Celery 应用程序。 Add to your web_spider/app/__init__.py or to web_spider/app/task_runner/__init__.py:添加到您的 web_spider/app/__init__.py 或 web_spider/app/task_runner/__init__.py:

from app.task_runner.celery_app import celery_app

__all__ = ('celery_app',)

It's documented at https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html#django-first-steps search for shared_task .它记录在https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html#django-first-steps search for shared_task

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM