Flask, blueprints uses celery task and got cycle import Flask, blueprints uses celery task and got cycle import flask flask

Flask, blueprints uses celery task and got cycle import


I don't have the code to try this out, but I think things would work better if you move the creation of the Celery instance out of tasks.py and into the create_app function, so that it happens at the same time the app instance is created.

The argument you give to the Celery worker in the -A option does not need to have the tasks, Celery just needs the celery object, so for example, you could create a separate starter script, say celery_worker.py that calls create_app to create app and cel and then give it to the worker as -A celery_worker.cel, without involving the blueprint at all.

Hope this helps.


What I do to solve this error is that I create two Flask instance which one is for Web app, and another is for initial Celery instance.

Like @Miguel said, I have

  • celery_app.py for celery instance
  • manager.py for Flask instance

And in these two files, each module has it's own Flask instance.

So I can use celery.task in Views. And I can start celery worker separately.


Thanks Bob Jordan, you can find the answer from https://stackoverflow.com/a/50665633/2794539,

Key points:
1. make_celery do two things at the same time: create celery app and run celery with flask content, so you can create two functions to do make_celery job
2. celery app must init before blueprint register