I have a Flask application, which is registered as follows:
APP = Flask(__name__)
APP.config.from_object('config')
I have defined a view for a URL, in which a function is called, which interacts with the DB.
from tasks import some_func
.
.
.
some_func.delay(params)
In the tasks.py file, I am creating a Celery instance as follows:
# Config values don't work here
celery = Celery('tasks', broker='amqp://', backend='amqp://')
.
.
.
@celery.task()
def some_func():
#DB interactions
Now I get an error that says:
RuntimeError: Working outside of application context.
I read about application contexts and how they can be used. I have imported current_app
in my tasks.py
file and tried using the context as follows:
@celery.task()
def some_func():
with current_app.app_context():
#DB interactions
However I still get the same error. I also tried pushing the context from the main file as follows:
ctx = APP.app_context()
ctx.push()
But no luck yet.
How do I make Celery work with Flask?
Note: I have already tried their example here.