我正在使用Miguel Grinberg的文章使用应用工厂模式设置Celery,以便使用Flask-Mail发送电子邮件.我一直在调用使用Celery的各种脚本而没有任何问题.但是Runtime Error: working outside of application context
,即使我在应用程序上下文中运行worker,我也会继续执行以下任务.为什么我收到此错误?如何让Flask-Mail在Celery中运行?
email.py
:
from flask import current_app, render_template
from flask.ext.mail import Message
from . import celery, mail
@celery.task
def send_async_email(msg):
mail.send(msg)
def send_email(to, subject, template, **kwargs):
with current_app.test_request_context(): # used app_context() as well.
msg = Message(current_app.config['PORTAL_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
sender=current_app.config['PORTAL_MAIL_SENDER'], recipients=[to])
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
send_async_email.delay(msg)
Run Code Online (Sandbox Code Playgroud)
__init__.py
:
from flask import Flask
from celery import Celery
from flask.ext.mail import …
Run Code Online (Sandbox Code Playgroud)