Python瓶与芹菜出应用程序上下文 [英] Python Flask with celery out of application context
问题描述
直到我试图用芹菜发送的邮箱发送一封邮件时,情况还是不错的。
完整追踪是
现在我得到一个在应用程序上下文以外工作的错误。 Traceback(最近一次调用的最后一个):
在trace_task $ b $中的文件/usr/lib/python2.7/site-packages/celery/task/trace.py,第228行b R = retval = fun(* args,** kwargs)
文件/usr/lib/python2.7/site-packages/celery/task/trace.py,行415,在__protected_call__
返回self.run(* args,** kwargs)
文件/home/ryan/www/CG-Website/src/util/mail.py,第28行,在send_forgot_email
msg =消息(恢复你的十字军游戏帐户)
文件/usr/lib/python2.7/site-packages/flask_mail.py,第178行,在__init__
sender = current_app.config.get (DEFAULT_MAIL_SENDER)
文件/usr/lib/python2.7/site-packages/werkzeug/local.py,第336行,在__getattr__
中返回getattr(self._get_current_object(),name )
文件/usr/lib/python2.7/site-packages/werkzeug/local.py,第295行,在_get_current_object
返回self .__ local()
文件/usr/lib/python2.7/site-packages/flask/globals.py,第26行,在_find_app
中引发RuntimeError('working outside of application context ')
RuntimeError:在应用程序上下文之外工作
这是我的邮件功能:
$ p code $ @ $ ce $
$ send $ for $ email
msg = Message(Recover your Crusade Gaming Account)
msg.recipients = [email]
msg.sender =Crusade Gaming stuff@cg.com
msg.html = \
你好人,< br />
您已要求重设密码。 < a href ={0}>点击此处恢复您的帐户< / a>或者将此链接复制并粘贴到您的浏览器中:{0}< br />
如果您没有要求重设密码,请忽略此项。
.format(url_for('account.forgot',ref = ref,_external = True))
mail.send(msg)
pre>
这是我的芹菜文件: $ _
$ b
from __future__ import absolute_import芹菜
$ 'src.util.mail'])
if __name__ ==__main__:
celery.start()
Flask邮件需要Flask应用程序上下文才能正常工作。在芹菜端实例化应用程序对象并使用应用程序.app_context是这样的:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / code>
I am building a website using python Flask. Everything is going good and now I am trying to implement celery.
That was going good as well until I tried to send an email using flask-mail from celery. Now I am getting an "working outside of application context" error.
full traceback is
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/celery/task/trace.py", line 228, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/celery/task/trace.py", line 415, in __protected_call__
return self.run(*args, **kwargs)
File "/home/ryan/www/CG-Website/src/util/mail.py", line 28, in send_forgot_email
msg = Message("Recover your Crusade Gaming Account")
File "/usr/lib/python2.7/site-packages/flask_mail.py", line 178, in __init__
sender = current_app.config.get("DEFAULT_MAIL_SENDER")
File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 295, in _get_current_object
return self.__local()
File "/usr/lib/python2.7/site-packages/flask/globals.py", line 26, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
This is my mail function:
@celery.task
def send_forgot_email(email, ref):
global mail
msg = Message("Recover your Crusade Gaming Account")
msg.recipients = [email]
msg.sender = "Crusade Gaming stuff@cg.com"
msg.html = \
"""
Hello Person,<br/>
You have requested your password be reset. <a href="{0}" >Click here recover your account</a> or copy and paste this link in to your browser: {0} <br />
If you did not request that your password be reset, please ignore this.
""".format(url_for('account.forgot', ref=ref, _external=True))
mail.send(msg)
This is my celery file:
from __future__ import absolute_import
from celery import Celery
celery = Celery('src.tasks',
broker='amqp://',
include=['src.util.mail'])
if __name__ == "__main__":
celery.start()
Flask-mail needs the Flask application context to work correctly. Instantiate the app object on the celery side and use app.app_context like this:
with app.app_context():
celery.start()
这篇关于Python瓶与芹菜出应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!