多进程 Gunicorn 的日志为什么不会错乱?

2016-11-15 12:12:50 +08:00
 TheCure

我的 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.

求详细解释,谢谢

5515 次点击
所在节点    Python
5 条回复
janxin
2016-11-15 13:14:32 +08:00
因为子进程共享了父进程的文件句柄
9hills
2016-11-15 13:18:02 +08:00
Linux 有个特性,你使用 APPEND 方式打开文件句柄,每次写入的量小于 PIPE_BUF_MAX ,那么系统能保证多进程之间不冲突
wwqgtxx
2016-11-15 14:30:10 +08:00
因为 linux 控制台保证了输出的进程安全
ysymi
2018-02-13 15:03:17 +08:00
作者没有遇到多个日志文件切分时 有的 worker 进程还往老的文件里写的问题吗
motianya211314
2019-04-12 11:46:00 +08:00
@ysymi 多个进程,每个进程都重复切割一次,导致已切割的日志被删除。你是咋解决的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/320631

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX