跳转到主要内容

TQDM进度条助手,用于日志记录和其他无头应用程序

项目描述

tqdm-loggable

tqdm-loggable 是一个小巧的Python包,提供日志友好的TQDM进度条。

如果你的Python应用程序有 tqdm 进度条,并在像以下这样的非交互式会话中使用它们...

  • 后台工作进程
  • Docker容器
  • 边缘计算
  • LogstashSentryDatadog 或其他外部日志工具
  • 长时间运行的机器学习任务
  • ...或者 stdout 流不可用或已重定向

...您不能拥有交互式进度条。当您使用监控工具观察应用程序时,通常在应用程序有tqdm进度进行时,您不会看到任何活动。如果进度条操作需要几分钟,则您的应用程序可能看起来已冻结。这可以通过tqdm-logging来解决,它会通过向文件和日志监控工具等日志后端发送常规报告来报告进度。

在这些情况下,tqdm-loggable会自动将您的tqdm进度条转换为可在无头系统中读取的可记录进度消息。

tqdm-loggable...

  • 是正常tqdm的替代品 - 除非检测到非交互式会话,否则不会发生变化
  • tqdm.auto和Jupyter Notebooks中的基于HTML的进度条兼容
  • 使用Python logging子系统来报告状态而不是终端
  • 每X秒打印一条日志行
  • 日志消息是结构化的,因此它们可以与Sentry、LogStash等高级日志服务一起工作,这些服务提供基于变量的高级搜索和标记
  • 特别支持Github Actions和其他持续集成环境

以下是在纯文本日志中输出tqdm日志消息的示例

tqdm_logging.py     :139  2022-09-21 12:13:44,138 Progress on:Progress bar without total -/- rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py     :139  2022-09-21 12:13:46,225 Progress on:Progress bar without total 10000/- rate:- remaining:? elapsed:00:02 postfix:-

tqdm_logging.py     :139  2022-09-21 12:13:46,225 Progress on:Sample progress -/60000 rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py     :139  2022-09-21 12:13:56,307 Progress on:Sample progress 21.0kit/60.0kit rate:1,982.9it/s remaining:00:19 elapsed:00:10 postfix:Current time=2022-09-21 10:13:55.801787
tqdm_logging.py     :139  2022-09-21 12:14:06,392 Progress on:Sample progress 41.0kit/60.0kit rate:1,984.1it/s remaining:00:09 elapsed:00:20 postfix:Current time=2022-09-21 10:14:05.890220

请注意,tqdm-loggabletqdm.contrib.logging不同,它采用不同的方法来解决不同的问题。

安装

包名为tqdm-loggable.有关如何在您的系统上安装包的说明,请阅读Python打包手册

用法

您需要做的唯一事情

  • 确保您的Python日志系统配置正确
  • 将导入从from tqdm.auto import tqdm更改为from tqdm_loggable.auto import tqdm
  • 可选:在应用程序初始化时调用tqdm_logging.set_level()
  • 可选:在应用程序初始化时调用tqdm_logging.set_log_rate()

Python代码库的搜索和替换说明

from tqdm import tqdm -> from tqdm_loggable.auto import tqdm 
from tqdm.auto import tqdm -> from tqdm_loggable.auto import tqdm

以下是一个示例脚本

import datetime
import logging
import time

from tqdm_loggable.auto import tqdm
from tqdm_loggable.tqdm_logging import tqdm_logging


logger = logging.getLogger(__name__)


def main():
    fmt = f"%(filename)-20s:%(lineno)-4d %(asctime)s %(message)s"
    logging.basicConfig(level=logging.INFO, format=fmt, handlers=[logging.StreamHandler()])

    # Set the log level to all tqdm-logging progress bars.
    # Defaults to info - no need to set if you do not want to change the level
    tqdm_logging.set_level(logging.INFO)
    
    # Set the rate how often we update logs
    # Defaults to 10 seconds - optional
    tqdm_logging.set_log_rate(datetime.timedelta(seconds=10))    

    logger.info("This is an INFO test message using Python logging")

    with tqdm(total=60_000, desc="Sample progress", unit_scale=True) as progress_bar:
        for i in range(60_000):
            progress_bar.update(1000)

            # Test postfix output
            progress_bar.set_postfix({"Current time": datetime.datetime.utcnow()})

            time.sleep(0.5)

tqdm_loggable会检测非交互式会话。如果应用程序在没有适当终端的情况下运行,则将使用非交互式进度消息。否则,进度条将委托给tqdm.auto模块以保持与任何tqdm系统兼容,而无需对代码进行任何更改。

tqdm_loggable还为Jupyter Notebook环境(如Datalore)提供进度条解决方案,这些环境与原始Jupyter Notebook不完全兼容。

用于记录消息的Python日志实例名为tqqm_loggable.tqm_logging

开发

注意:此存储库活动度很低,我们不积极监控新问题。如果您有问题或PR,请在Discord中联系我们

您可以使用tqdm_loggable/manual_tests.py运行各种检查,以查看不同的交互式和非交互式会话会给出什么结果。

# Normal interactive terminal run
poetry run manual-tests 

因为这是一个正常的shell会话,所以您将获得正常的进度条

Sample progress:  20%|████████▏                                | 12.0k/60.0k [00:05<00:24, 1.98kit/s, Current time=2022-09-21 15:40:24.274670]

...然后在没有适当的TERM环境变量的情况下进行测试

# Disable interactive terminal by fiddling with TERM environment variable
TERM=dumb poetry run manual-tests 

您将获得日志消息

tqdm_logging.py     :139  2022-09-21 17:41:20,720 Progress on:Sample progress -/60000 rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py     :139  2022-09-21 17:41:30,803 Progress on:Sample progress 21.0kit/60.0kit rate:1,984.7it/s remaining:00:19 elapsed:00:10 postfix:Current time=2022-09-21 15:41:30.300714

...或者使用不同的Docker会话

# This will display process as log messages
docker build -t manual-tests . && docker run manual-tests

# This will allocate a terminal and display progress as a normal tqdm progress bar
docker build -t manual-tests . && docker run -ti manual-tests

或者使用重定向的stdout

poetry run manual-tests > output.txt
cat output.txt

这将输出我们的终端检测信息并绘制一个进度条,总共30秒。

tqdm-loggable manual tests
sys.stdout.isatty(): False
TERM: -
is_interactive_session(): False

然后根据您是否交互式运行手动测试,将跟随进一步的进度条或进度消息。

另请参阅

查看其他相关的日志包

致敬

最初为Trading Strategy区块链交易自动化构建。

查看原始StackOverflow问题.

许可证

MIT

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

tqdm_loggable-0.2.tar.gz (7.4 kB 查看哈希值)

上传时间 源代码

构建分发

tqdm_loggable-0.2-py3-none-any.whl (9.3 kB 查看哈希值)

上传时间 Python 3

由以下支持