我的 flask 项目使用 logging 模块,使用 TimedRotatingFileHandler,然后 gunicorn 开了 32 个进程.
logging 模块不是进程安全的,这个 logging 模块的官网有写
Because there is no standard way to serialize access to a single file across multiple processes in Python. If you need to log to a single file from multiple processes, one way of doing this is to have all the processes log to a SocketHandler, and have a separate process which implements a socket server which reads from the socket and logs to file. (If you prefer, you can dedicate one thread in one of the existing processes to perform this function.)
但是我的项目跑了半年,从来没有看到日志错乱的情况,每一条都清清楚楚..
Google 了下,搜到了一个 gunicorn 的 issue:https://github.com/benoitc/gunicorn/issues/1272
但是看完了这个 issue 表示还是云里雾里没懂,gunicorn 作者给的解释如下
The fd is shared between all workers, and until it isn't over the limit (depending on your system) alls logs will go over it.
求详细解释,谢谢
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.