日志处理程序是否使用单独的线程?
Python 的日志处理程序非常棒。其中一些,例如 SMTPHandler 可能需要很长时间才能执行(联系 SMTP 服务器等)。它们是否在单独的线程上执行,以免阻塞主程序?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SMTPHandler 使用 smtplib 当使用此库发送电子邮件时,您的进程将被阻止,直到它已正确发送,未创建线程。
如果您不想在发送电子邮件时阻止进程,则必须实现自己的 SMTPHandler 并重写
emit(self, record)
方法。阻塞较少的处理程序是 SysLogHandler ,因为它通常是本地通信,并且在 UDP 中,因此系统不会等待来自目的地的任何确认。
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.
不,据我所知,您应该生成一个单独的进程。
No, you should spawn a separate process, as far as I know.