日志处理程序是否使用单独的线程? [英] Do logging handlers use separate threads?

查看:101
本文介绍了日志处理程序是否使用单独的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的日志处理程序很棒.其中的一些,例如 SMTPHandler 可能需要很长时间才能执行(联系SMTP服务器及其他).它们是否在单独的线程上执行以免阻塞主程序?

解决方案

SMTPHandler 在发送带有以下内容的电子邮件时,使用 smtplib 此库,直到您的进程被正确发送,直到没有创建线程为止,该进程才被阻塞.

如果您不想在发送电子邮件时阻止进程,则必须实现自己的SMTPHandler并覆盖emit(self, record)方法.

阻塞较少的处理程序是 SysLogHandler ,因为它通常是本地通信,并且使用UDP,因此系统不会等待来自目的地的任何确认.

Python's logging handlers are great. Some of them, such as the SMTPHandler may take a long while to execute (contacting an SMTP server and all). Are they executed on a separate thread as to not block the main program?

解决方案

SMTPHandler uses smtplib and when sending an email with this library, your process is blocked until it have been correctly sent, no thread created.

If you do not want to block your process when sending an email, you'll have to implement your own SMTPHandler and override the emit(self, record) method.

The less blocking handler is the SysLogHandler, because it is in general a local communication, and in UDP so the system doesn't wait for any acknowledgement from the destination.

这篇关于日志处理程序是否使用单独的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆