supervisord管理python进程时print(stdout)日志输出不及时,甚至有缺失问题解决

 python, supervisord  supervisord管理python进程时print(stdout)日志输出不及时,甚至有缺失问题解决已关闭评论
12月 192019
 

使用supervisord管理python进程,代码中print()打印的日志在使用tail -f查看时输出总是不及时,甚至有缺失问题,网络上有下面的几种方式解决:

If you are also using Supervisor to monitor and heal your long running Python projects and observed that output of your program is not being logged to stdout_logfile, it is because Python print statement does not automatically flush output to STDOUT.

One solution is using sys.stdout.flush() frequently to flush the output or if you are using Python 3.3, print(msg, flush=True) is another solution. However, a better solution is to run python with -u parameter (unbuffered mode).

以上总结方法:

  1. 频繁使用sys.stdout.flush()
  2. python3.3以后版本,print使用print(msg, flush=True)
  3. supervisord配置文件里command命令行添加-u参数(这个是最推荐的方法)

举例:

[program:analysis]
command = python -u AnalysisTest.py -port=70%(process_num)02d