python - 即使使用 app.app_context(),如何修复“RuntimeError:在应用程序上下文之外工作”?
问题描述
[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.
解决方案
推荐阅读
- php - 如何链接到页面的特定部分 HTML PHP
- c# - 当后端代码中的简单键值变量工作相同时,为什么要使用 redis 作为缓存层?
- google-apps-script - Google Slides Apps 脚本检索页面中的形状
- python-3.x - 在 KNeighborsClassifier 中使用自定义指标时,我不断收到“TypeError:只有整数标量数组可以转换为标量索引”
- java - JavaFX - 在使用选项卡时将 GUI 拆分为单独的类
- jquery - 通过函数调整电子窗口大小
- scheme - 匹配方案中的括号
- android - 当我使用 adb 显示 /sys/kernel/debug/tracing/available_filter_functions 时没有这样的设备
- python - 初始化以迭代抽象pyomo模型中的集合?
- angular-cli - 运行 ng serve -o 时出现错误