logging - How to show tqdm progress in MetaFlow?
问题描述
When running MetaFlow Flows, tqdm
progress bars do not get displayed until the final iteration, which defeats the purpose of measuring progress. Is there a way to force MetaFlow to print out tqdm updates?
解决方案
The problem is that tqdm
writes to stderr, but MetaFlow hides output to stderr. The solution requires two tricks: using a context manager to redirect tqdm
outputs to a logger, and setting that logger to write to stderr.
Example:
import logging
import sys
from time import sleep
import tqdm
from metaflow import FlowSpec, step
from tqdm.contrib.logging import tqdm_logging_redirect
# stream to stdout needed because MetaFlow hides output to stderr :C
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
class TQDMFlow(FlowSpec):
@step
def start(self):
print("Training...")
with tqdm_logging_redirect(): # this context manager redirects tqdm output to logging
for _ in tqdm.tqdm(range(20)):
sleep(0.25)
self.next(self.end)
@step
def end(self):
pass
if __name__ == "__main__":
TQDMFlow()
I also tried redirecting output directly to stdout (without the tqdm_logging_redirect
context manager or logging
) using tqdm(range(n), file=sys.stdout)
but that did not work.
推荐阅读
- android - Android drag to pan, navigate through app
- python - Go to definition is not working with docker and vagrant
- xmpp - 实现无状态 ejabberd 架构所需的建议或帮助
- python - How do you determine what attributes and methods are available in PloneFormGen
- c# - .Net Core Docker Image 在 docker 中运行时无法访问数据库
- java - 将 T 参数转换为适当的具体类型
- c# - OPOS Bridge for UWP PointOfService 是否支持 OPOS 条码扫描仪
- python - 在 Python 的 zipfile 中提取 txz 文件
- javascript - 为什么我在 JSS 样式表中声明的 woff 没有加载 @font-face?
- python - 在 python 中有没有 exec 的替代方法?