在主線程中再開啟一個線程,由于線程隔離機制,是找不到核心對象的朴皆,所以需要自己手動找到核心對象
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()