为什么“docker run t”可以让python刷新输出缓冲区?

2024-10-01 22:42:04 发布

您现在位置:Python中文网/ 问答频道 /正文

1。在

Dockerfile:

FROM python:3
CMD ["python", "-m", "http.server"]

当我执行next时,您可以看到没有可以看到的日志。在

^{pr2}$

2。在

我与上面的问题斗争了一段时间,最后发现是因为程序缓冲区问题,所以我可以用next来解决:

Dockerfile:

FROM python:3
CMD ["python", "-u", "-m", "http.server"]

现在使用-u

shubuntu1@shubuntu1:~/77$ docker build -t a:1 . --no-cache
...
Successfully tagged a:1
shubuntu1@shubuntu1:~/77$ docker rm -f test
test
shubuntu1@shubuntu1:~/77$ docker run -d --name test a:1
68bc759a54ec3218b39e51404495a28d010a798b1d1e160ec7f68be3b18da9c7
shubuntu1@shubuntu1:~/77$ docker logs test
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

3。在

但是当我用情况1回滚Dockerfile时:

Dockerfile:

FROM python:3
CMD ["python", "-m", "http.server"]

我发现-tdocker run中,缓冲区也会刷新如下:

shubuntu1@shubuntu1:~/77$ docker build -t a:1 . --no-cache
...
Successfully tagged a:1
shubuntu1@shubuntu1:~/77$ docker rm -f test
test
shubuntu1@shubuntu1:~/77$ docker run -dt --name test a:1
f7cd1b5b3c272ff42c7aecd251e324b70030c046489048370689ba25b33191cc
shubuntu1@shubuntu1:~/77$ docker logs test
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

所以,我的问题是:为什么-t会有这种效果?在


Tags: dockernorunfromtestdockerfilebuildcmd
1条回答
网友
1楼 · 发布于 2024-10-01 22:42:04

运行带有-t/ tty的docker容器会将一个伪终端附加到容器中的进程。在

如果一个stdout描述符被附加到终端上,它将被缓存。
假设用户正在监视终端以获得进程的输出;频繁地打印数据被认为是重要的。一旦遇到换行符(\n)字符、缓冲区被填满或进程结束,缓冲区就会被刷新。在

如果stdout附加到不同于终端的东西,那么流将被完全(或块)缓冲。在libc/glibc上,默认缓冲区大小为4096字节。缓冲区必须在其内容被刷新之前填满。
当假定写入stdout的数据稍后会被检查(例如日志文件),这就减少了昂贵的写入系统调用的次数。在

另见埃文·克里茨克的Stdout Buffering,pádraig Brady的Buffering in standard streams和{a3}。在

相关问题 更多 >

    热门问题