在如何实现异步发送邮件的时候,遇到过这样一个报错
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that
needed an active HTTP request. Consult the documentation on testing
for information about how to avoid this problem.
当时虽然是在请求里使用的curent_app
方法,已经有了请求上下文和应用上下文,但是任然报错,终究原因是没有记住文档上说的current_app
的含义,它只是个代理,每个线程都是隔开的,一个线程一个id;它是根据id去找的,就好比说一开始current_app的id是1,然后新开了一个线程,新线程的id是2,那我在2里使用1,肯定就找不到喽,如何解决文档上也有说,使用current_app._get_current_object()
就可以了,
代码里就是这样,而不是直接传递current_app
: