先看报错:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in some way.
To solve this, set up an application context with app.app_context().
RuntimeError (_app_ctx_err_msg): Working outside of application context while sending mail from thread
raise RuntimeError(_app_ctx_err_msg)
在flask中,前端发起请求起一个耗时的任务,后端处理逻辑不可能等待该任务执行完成再返回结果,那样前端会卡死,所以用线程去起那个耗时的任务,问题来了,该耗时任务结束后会报上下文错误,
解决方法如下:
其中current_app是通过 from flask import current_app导入, current_app._get_current_object()作为线程参数传递,然后在deal_ordertasks这个耗时任务中加上 with app.app_context():即可。
类似的,还有发送邮件也可以参考此方法
最后感谢分享的各位大神
https://stackoverflow.com/questions/55417236/runtimeerror-app-ctx-err-msg-working-outside-of-application-context-while-se