从多进程损坏的文件记录?

Logging from multi processes corrupt file?

我使用 Django 通过 gunicorn 服务,有四个 gevent worker 并且有一些日志配置,特别是它使用 RotatingFileHandler。 python 文档指出:

Although logging is thread-safe, and logging to a single file from multiple threads in a single process is supported, logging to a single file from multiple processes is not supported, because there is no standard way to serialize access to a single file across multiple processes in Python.

因此,在我当前的配置中,gunicorn 只有一个父进程并且有一些线程,因此日志记录不应损坏文件,但在我的生产中我的备份文件已损坏但是无法在我的本地设置中重现这种情况。

我应该假设配置没问题并继续使用它还是应该采用不同的方法(例如 multiprocessing.QueueSocketHandler)?

如果您有四个事件工作者,这意味着有四个不同的进程执行请求处理和日志记录 - 因此您可能应该按照 here. I usually use Supervisor 对 运行 侦听器的描述使用 SocketHandler过程。