我有一个 Flask 应用程序,其中 celery 可以正常工作,而 Flask-Mail 本身也可以正常工作。
from celery import Celery
from flask_mail import Mail, Message
app = Flask(__name__)
mail = Mail(app)
celery = Celery('main_app',
broker='mongodb://localhost',
backend='mongodb://localhost')
@celery.task
def cel_test():
return 'cel_test'
@app.route('/works_maybe')
def works_maybe():
return cel_test.delay()
到目前为止,一切都很好
cel_test 与 celery worker 配合得很好;一切都出现在 mongo 中。
但这就是奇怪的地方。“注册”加邮件方法没有 100% 有效@celery.task
,但当它成为任务时就会崩溃。
@celery.task
def send_email(some_arg, name, email):
msg = Message(…message details..)
return mail.send(msg)
@app.route("/signup", methods=['POST'])
def signup():
return send_email.delay(...stuff for the message…)
痕迹
R = retval = fun(*args, **kwargs)
File "/Users/username/pymods/virtualenvs/directory/lib/python2.7/site-packages/celery-3.0.15-py2.7.egg/celery/task/trace.py", line 415, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/username/pymods/directory/directory/main_app/main_app.py", line 43, in send_email
something = 'a string in the Message'),
File "/Users/username/pymods/virtualenvs/directory/lib/python2.7/site-packages/flask/templating.py", line 123, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
有人可以解释为什么在一种情况下芹菜效果很好,但是当我涉及 mail.send(msg) 时它会中断?
也许我需要更普遍地学习 python 的东西?
任何想法,如果至少是关于处理此类问题的方法,将不胜感激。