在 python 中记录多线程进程

发布于 2024-08-04 08:20:34 字数 89 浏览 1 评论 0原文

我正在考虑使用日志记录模块将所有事件记录到一个文件中。线程数量从开始到结束应该是恒定的,但如果一个线程失败,我想只记录它并继续。实现这一目标的简单方法是什么?谢谢!

I was thinking of using the logging module to log all events to one file. The number of threads should be constant from start to finish, but if one thread fails, I'd like to just log that and continue on. What's a simple way of accomplishing this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-08-11 08:20:34

不完全确定“一个线程失败”是什么意思,但是如果“失败”意味着异常一直传播到线程的顶级函数,那么您可以包装每个线程的顶级函数(例如在装饰器中) ) 捕获任何异常,记录您想要的任何内容,然后重新引发。 logging 模块应确保日志记录操作的线程安全性,而您无需采取进一步的预防措施。

Not entirely sure what you mean by "one thread fails", but if by "fail" you mean that an exception propagates all the way up to the top function of the thread, then you can wrap every thread's top function (e.g. in a decorator) to catch any exception, log whatever you wish, and re-raise. The logging module should ensure thread-safety of logging actions without further precautions being needed on your part on that score.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文