0
[2018-05-29 14:25:35,490: ERROR/ForkPoolWorker-1] Task utils.send_mail[cffafdb2-f242-4ecd-b42c-03f701d700d8] raised unexpected: RuntimeError('Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nto interface with the current application object in a way.  To solve\nthis set up an application context with app.app_context().  See the\ndocumentation for more information.',)
Traceback (most recent call last):
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/celery/app/trace.py", line 375, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/celery/app/trace.py", line 632, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/luklas/Code/GitHub/ctflearn-flask/utils.py", line 25, in send_mail
    msg = Message(subject, recipients=recipients)
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/flask_mail.py", line 273, in __init__
    sender = sender or current_app.extensions['mail'].default_sender
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/Users/luklas/Code/GitHub/ctflearn-flask/venv/lib/python3.6/site-packages/flask/globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in a way.  To solve
this set up an application context with app.app_context().  See the
documentation for more information.

utils.py

from ctflearn import db, celery, app, mail
@celery.task
def send_mail(subject, body, recipients):
    msg = Message(subject, recipients=recipients)
    msg.html = body
    with app.app_context():
        mail.send(msg)

init.py

from flask import Flask, Blueprint
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_pagedown import PageDown
from celery import Celery

app = Flask(__name__, static_url_path='/static', template_folder='static/templates')

app.config.from_object('config')

app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "signin"

db = SQLAlchemy(app)
mail = Mail(app)

pagedown = PageDown(app)
...

Why am i still getting this error even though I get an app context? I would rather stay away from the flask factory design. The celery worker picks up this task without a problem, but it throws an exception saying the code is running outside of an application context, even though I run it inside an app context.

4

0 回答 0