在运行MetaFlow Flow时,tqdm
进度条直到最后一次迭代时才会显示,这违背了测量进度的目的。有没有办法强制MetaFlow打印出tqdm更新?
发布于 2021-07-02 21:14:11
问题是tqdm
写入标准错误代码,但MetaFlow将输出隐藏到标准错误代码。该解决方案需要两个技巧:使用上下文管理器将tqdm
输出重定向到记录器,并将记录器设置为写入标准错误。
示例:
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()
我还尝试使用tqdm(range(n), file=sys.stdout)
将输出直接重定向到标准输出(不使用tqdm_logging_redirect
上下文管理器或logging
),但不起作用。
https://stackoverflow.com/questions/68225881
复制相似问题