Flask with Celery - Application context not available
Context:The same error happened to me while trying to send email(s) through Celery
tasks. In my case, I was sending emails that was rendered by Flask
with html templates.
Reason:Celery
and Flask
are disconnected. Thus, Celery has no clue about application context of Flask. I had to manually inject application context.
Solution:What worked for me was Flask's app_context()
function. Simply inject/wrap application context around the function where error originate from.
Example:
from app import appfrom app import mail@celery.taskdef sign_up_task(**config, **context): mail = Mail() with app.app_context(): mail.send(**config, **context)