使用Flask-Mail异步导致“RuntimeError:在应用程序上下文之外工作” [英] Using Flask-Mail asynchronously results in "RuntimeError: working outside of application context"

查看:772
本文介绍了使用Flask-Mail异步导致“RuntimeError:在应用程序上下文之外工作”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一些邮件异步(基于在 The Flask Mega-Tutorial,Part XI:Email Support )。但是,我得到以下关于在应用程序上下文之外工作的错误。如何解决这个问题?
$ b $ pre $ Traceback(最近一次调用最后一次):
文件C:\ Users \Primoz\Desktop\RecycleFlaskServer\recycleserver\helpers.py,第17行,在send_async_email
mail.send(msg)
文件C:\Python33\lib\ site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py,第434行,发送
message.send(连接)
文件C:\Python33\\ \\ lib \site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py,第369行,发送
connection.send(self)
文件C: \Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py,第173行,发送
email_dispatched.send(message,app = current_app。 _get_current_object())
文件C:\Python33\lib\site-packages\werkzeug-0.9.4-py3.3.egg\werkzeug\local.py,第297行,在_get_current_object
返回self .__ local()
文件C:\Python33\\ \\ lib \ site-packages \flask-0.10.1-py3.3.egg\flask\globals.py,第34行,在_find_app
中引发RuntimeError('在应用程序上下文之外工作')
RuntimeError:在应用程序环境之外工作





  from flask import Flask 
from flask.ext.mail import Mail
app = Flask(__ name__)
app.config.from_object('recycleserver.settings')
mail =应用程序

def async(f):
def wrapper(* args,** kwargs):
thr = Thread(target = f, args = args,kwargs = kwargs)
thr.start()
返回包装
$ b @async
def send_async_email(msg):
mail.send (msg)

def send_simple_mail(subject,sender,to_who,text_body =,html_body =):
msg =信息(subject = subject,sender = sender,recipients = to_who )
msg.body = text_body
msg.html = html_body
send_async_email(msg)


< div类=h2_lin>解决方案

代码应该在应用上下文中运行。用app.app_context()添加()
$ b

  @async $ b $ ():
mail.send(msg)


send_async_email

I am trying to send some mail asynchronously (based on the code in The Flask Mega-Tutorial, Part XI: Email Support). However, I get the following error about working outside an application context. How do I fix this problem?

Traceback (most recent call last):
  File "C:\Users\Primoz\Desktop\RecycleFlaskServer\recycleserver\helpers.py", line 17, in send_async_email
    mail.send(msg)
  File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 434, in send
    message.send(connection)
  File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 369, in send
    connection.send(self)
  File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 173, in send
    email_dispatched.send(message, app=current_app._get_current_object())
  File "C:\Python33\lib\site-packages\werkzeug-0.9.4-py3.3.egg\werkzeug\local.py", line 297, in _get_current_object
    return self.__local()
  File "C:\Python33\lib\site-packages\flask-0.10.1-py3.3.egg\flask\globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

from flask import Flask
from flask.ext.mail import Mail
app = Flask(__name__)
app.config.from_object('recycleserver.settings')
mail = Mail(app)

def async(f):
    def wrapper(*args, **kwargs):
        thr = Thread(target = f, args = args, kwargs = kwargs)
        thr.start()
    return wrapper

@async
def send_async_email(msg):
    mail.send(msg)

def send_simple_mail(subject, sender, to_who, text_body="", html_body=""):
    msg = Message(subject=subject,sender=sender, recipients=to_who)
    msg.body = text_body
    msg.html = html_body
    send_async_email(msg)

解决方案

The code should run in an app context. Add with app.app_context():

 @async
 def send_async_email(msg):
    with app.app_context():
       mail.send(msg)

这篇关于使用Flask-Mail异步导致“RuntimeError:在应用程序上下文之外工作”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆