在主线程中再开启一个线程,由于线程隔离机制,是找不到核心对象的,所以需要自己手动找到核心对象
from flask import current_app, render_template
def send_async_email(app, msg):
with app.app_context():
try:
mail.send(msg)
except Exception as e:
pass
# 这种方式获取flask核心对象app
app = current_app._get_current_object()
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()