多线程系统中的测井设计模式

2024-04-26 12:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在多线程环境中利用设计模式生成日志。有多个线程需要在日志中写入这个文件。因此,必须有一种机制,使每个线程在创建后都可以访问同一个文件处理程序。
我应该使用Singleton还是Factory设计模式,因为只有对象的安装点,还是有更好的方法来实现这一点。在


Tags: 文件对象方法处理程序利用环境factory设计模式
1条回答
网友
1楼 · 发布于 2024-04-26 12:16:16

Python logging模块实际上是thread-safe by default

The logging module is intended to be thread-safe without any special work needing to be done by its clients. It achieves this though using threading locks; there is one lock to serialize access to the module’s shared data, and each handler also creates a lock to serialize access to its underlying I/O.

相关问题 更多 >