简体   繁体   English

带有 Docker 日志的 tqdm 进度条

[英]tqdm progress bar with Docker logs

I am using tqdm to display various progress bars for my Python console application.我正在使用tqdm为我的 Python 控制台应用程序显示各种进度条。 For the production deployment of the applications, I use Docker.对于应用程序的生产部署,我使用 Docker。

The progress bars work fine when running a Python application in a terminal.在终端中运行 Python 应用程序时,进度条工作正常。 However, when Dockerized and the terminal output is accessed through docker logs the progress bar does not function because as far as I understand it is not an interactive terminal.但是,当 Dockerized 和终端 output 通过docker logs访问时,进度条不会 function 因为据我了解它不是交互式终端。 Although it looks like the progress gets rendered if docker logs is dumped after the progress bar have completed, but not sure if there are some other conditions for this to happen (output buffering?).虽然看起来如果docker logs在进度条完成后转储,则进度会呈现,但不确定是否还有其他条件会发生这种情况(输出缓冲?)。

I would like to modify my tqdm behavior so that我想修改我的tqdm行为,以便

  • It detects when it is run in non-interactive Dockerised environment它检测何时在非交互式 Dockerised 环境中运行
  • Instead of displaying interactive progress bar, it will log completion statements (10% done, X iterations/s) regularly它不会显示交互式进度条,而是定期记录完成语句(完成 10%,X 次迭代/秒)

This way the progress durations and such would be more accessible when running the application in production.这样,在生产中运行应用程序时,进度持续时间等将更容易访问。

What would be the way to attach such a custom behavior to tqdm ?将这种自定义行为附加到tqdm的方法是什么?

You can recognize if your process is running in non-interactive docker environment according to absence of TERM environment variable.您可以根据没有 TERM 环境变量来识别您的进程是否在非交互式 docker 环境中运行。

# interactive environment
$ docker run --rm -it centos:7 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=8c810e490671
TERM=xterm
HOME=/root

# non-interactive environment
$ docker run --rm  centos:7 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=e267f2ba0f8f
HOME=/root

Consider this fact in your script and in case of non-interactive environment use total parameter of tqdm or disable it:在您的脚本中考虑这一事实,如果是非交互式环境,请使用 tqdm 的total参数或disable它:

total: int or float, optional总计:int 或 float,可选

The number of expected iterations.预期的迭代次数。 If unspecified, len(iterable) is used if possible.如果未指定,则尽可能使用 len(iterable)。 If float("inf") or as a last resort, only basic progress statistics are displayed (no ETA, no progressbar).如果 float("inf") 或作为最后的手段,则仅显示基本进度统计信息(无 ETA,无进度条)。 If gui is True and this parameter needs subsequent updating, specify an initial arbitrary large positive number, eg 9e9.如果 gui 为 True 并且此参数需要后续更新,请指定一个初始的任意大正数,例如 9e9。

disable: bool, optional禁用:布尔,可选

Whether to disable the entire progressbar wrapper [default: False].是否禁用整个进度条包装器 [默认值:False]。 If set to None, disable on non-TTY.如果设置为无,则在非 TTY 上禁用。

https://github.com/tqdm/tqdm/#parameters https://github.com/tqdm/tqdm/#parameters

Update:更新:

It looks like TERM env variable in non-interactive environment has the value "dumb"看起来非交互式环境中的 TERM 环境变量的值为“哑”

docker run --rm  centos:7 bash -c 'echo "${TERM}"'
dumb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM