supervisord日志未显示我的输出

33

我有一个运行中的 [program:x],它会打印/ sys.stdout.write 很多东西。但是,这些内容都没有出现在[supervisord]的 AUTO childlogdir 或[program:x]的 stdout_logfile 中。我错过了什么吗?

我该如何捕获从 [program:x] 打印或输出的所有内容?

在我的程序中,我明确地做了这两件事,

print "something"
sys.stdout.write("something") 

相关的supervisord.conf文件

[supervisord]
childlogdir = %(here)s/../logs/supervisord/
logfile = %(here)s/../logs/supervisord/supervisord.log
logfile_maxbytes = 100MB
logfile_backups = 10
loglevel = info
pidfile = %(here)s/../logs/supervisord/supervisord.pid
umask = 022
nodaemon = false
nocleanup = false

[program:x]
directory = %(here)s/../
command = python file.py
autostart=true
autorestart=true
redirect_stderr=true  
stdout_logfile = /appropriate-path/to/access.log

编辑了问题,提供更多信息。当我使用sys.stdout.write时,它应该打印到标准输出 - zubinmehta
我放入了整个conf文件。此外,我尝试在file.py的末尾放置sys.stdout.flush(),但没有成功。 - zubinmehta
1
如果您检查了supervisord日志文件,您是否真的看到您的进程已经启动?如果程序从未运行过,则无法输出任何内容。 - Mark Hildreth
@MarkHildreth 我的程序运行良好。 - zubinmehta
也没有带有3.0b1版本的存储库。 - Martijn Pieters
显示剩余10条评论
3个回答

66

Python输出有缓冲。在您的supervisord.conf中设置环境变量PYTHONUNBUFFERED=1将禁用缓冲并更早地显示日志消息:

[program:x]
environment = PYTHONUNBUFFERED=1

或者在 python 命令中添加 -u 命令行选项

[program:x]
command = python -u file.py

或者您可以显式地清除sys.stdout处理程序:

sys.stdout.flush()

在 Python 3.3 及更高版本中,您可以添加 flush=True 参数让函数自动执行:

print(something, flush=True)

48

您可以按照以下方式运行您的程序:

python -u file.py

这将产生无缓冲输出


1
适当的 - Tim

1
如果您有基于Python的脚本,不能或不想更改以定期刷新输出,则可以使用Expect软件包中的unbuffer
对于在Docker容器中的Django应用程序,我最近是这样使用它的(从supervisord运行的shell脚本):
unbuffer python -u manage.py runserver 0.0.0.0:11000 2>&1 >> /var/log/django.log

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接