I have a Flask app, which is a very basic app with a POST handler and some DB insertions. The DB insertions are set as tasks using Celery. If I put my Celery instance creation and tasks definition in tasks.py
file, and call the functions from my main.py
file (which also has the Flask app creation), I get an out of context error
. The tasks in the tasks.py
file in turn call a DB class that does the DB insertions. How do I properly create the Celery instance and make sure it has the Flask context?
This is how the structure roughly resembles:
- main.py = Flask app creation, routes handling and tasks.delay calls.
- tasks.py = Celery instance creation and task definitions.
- DB = Inserts.
I want everything to work in the same context.