基本上,当线程中发生未处理的异常时,我希望使用Djangomail_admins
接收电子邮件。目前,我有一个处理post请求的视图,它看起来像:
import threading
# ...
def post(self, request, *args, **kwargs):
# Some stuff...
for message in entry['messaging']:
t = threading.Thread(
target=self.local_handler,
args=(message, page_id)
)
t.setDaemon(True)
t.start()
# Some more stuff
现在,当任何异常发生并且没有得到处理时,它将显示在服务器日志中。像这样:
^{pr2}$有没有办法通过电子邮件接收这些回溯? 在我开始使用线程之前,我一直在接收电子邮件,但现在它已经不起作用了。在
编辑:
正如@ivan miljkovic在回答一个类似问题时建议的那样,这个解决方案对我很有效。在
所以,基本上我必须在线程函数中捕捉异常:
import threading
from django.core.mail import mail_admins
# ...
def local_handler(self, message, page_id):
try:
# do stuff
except: # Any not handled exception will send an email with the traceback
import traceback
details = traceback.format_exc()
mail_admins('Background exception happened', details)
def post(self, request, *args, **kwargs):
# Some stuff...
for message in entry['messaging']:
t = threading.Thread(
target=self.local_handler,
args=(message, page_id)
)
t.setDaemon(True)
t.start()
# Some more stuff
试试这个。在
当你在线程的调用者中捕捉到它时,你可以像以前一样轻松地通过电子邮件发送它。在
Catch a thread's exception in the caller thread in Python
相关问题 更多 >
编程相关推荐