翼度科技»论坛 编程开发 python 查看内容

k8s pod log日志不显示python print打印的内容

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
问题

Python 程序代码中使用 print() 打印的内容,在查询 k8s pod 日志时不显示。
原因

print() 函数

Python 的 print() 函数签名如下:
  1. print(*objects, sep=' ', end='\n', file=None, flush=False)
复制代码
摘录部分官方文档如下:
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.
由于没有显式地传递 file 参数,所以 Python 会默认使用 sys.stdout
sys.stdout

查阅官方文档,节选片段:
When interactive, the stdout stream is line-buffered. Otherwise, it is block-buffered like regular text files. The stderr stream is line-buffered in both cases. You can make both streams unbuffered by passing the -u command-line option or setting the PYTHONUNBUFFERED environment variable.
stdout是有缓存的,只有遇到换行或者积累到一定的大小,才会显示出来。
解决方案

解决方案在 envvar-PYTHONUNBUFFERED 中其实已经说明了,有两种:

  • 设置环境变量 PYTHONUNBUFFERED
  • 运行 python 程序时,使用 -u 命令选项
在 Dockerfile 中我们只需这样一行设置即可:
  1. ENV PYTHONUNBUFFERED=1
复制代码
参考



来源:https://www.cnblogs.com/fengyinxu/p/python-print-no-show-in-k8s-pod-log.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具