简体   繁体   English

使用 nohup 时是否可以刷新 tqdm output

[英]Is it possible to flush tqdm output when using nohup

I use the python tqdm package for showing the progress of the execution of my code.我使用 python tqdm package 来显示我的代码执行进度。 Heres a small sample for explaining my use case better这是一个小样本,可以更好地解释我的用例

progress = tqdm(range(2))
for epoch in progress:
    loss = []
    for i in range(2):
        time.sleep(0.5)
        loss_ = random.random()
        progress.set_description("EPOCH: {}, LOSS: {}".format(epoch, loss_))
        loss.append(loss_)
    loss_mean = sum(loss) / len(loss)
    print("EPOCH: {}, LOSS: {}".format(epoch, loss_mean))

When i run this code without nohup, it runs as i expect.当我在没有 nohup 的情况下运行此代码时,它会按预期运行。 it refreshes tqdm descriptions and keeps only the last one on terminal.它刷新 tqdm 描述并只在终端上保留最后一个描述。 sample output样本 output

EPOCH: 0, LOSS: 0.9789279434307936:   0%|                                                 | 0/2 [00:01<?, ?it/s]
EPOCH: 0, LOSS: 0.5650528466113542
EPOCH: 1, LOSS: 0.2959674437384582:  50%|████████████████████▌                    | 1/2 [00:02<00:01,  1.00s/it]
EPOCH: 1, LOSS: 0.39633745424290057
EPOCH: 1, LOSS: 0.2959674437384582: 100%|█████████████████████████████████████████| 2/2 [00:02<00:00,  1.00s/it]

however when i run the same code using nohup for running the process in background, all the tqdm descriptions from the inner loop get printed and the outputs of the print statement are printed at the end.但是,当我使用 nohup 运行相同的代码以在后台运行该进程时,内部循环中的所有 tqdm 描述都会被打印出来,打印语句的输出会在最后打印出来。 Heres the output when using nohup.这是使用 nohup 时的 output。

  0%|          | 0/2 [00:00<?, ?it/s]
EPOCH: 0, LOSS: 0.6247515429741374:   0%|          | 0/2 [00:00<?, ?it/s]
EPOCH: 0, LOSS: 0.531051885429166:   0%|          | 0/2 [00:01<?, ?it/s]
EPOCH: 0, LOSS: 0.531051885429166:  50%| ^v^h ^v^h ^v^h ^v^h ^v^h     | 1/2 [00:01<00:01,  1.00s/it]
EPOCH: 1, LOSS: 0.4399544030856224:  50%| ^v^h ^v^h ^v^h ^v^h ^v^h     | 1/2 [00:01<00:01,  1.00s/it]
EPOCH: 1, LOSS: 0.6654644291991813:  50%| ^v^h ^v^h ^v^h ^v^h ^v^h     | 1/2 [00:02<00:01,  1.00s/it]
EPOCH: 1, LOSS: 0.6654644291991813: 100%| ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h| 2/2 [00:02<00:00, >
EPOCH: 1, LOSS: 0.6654644291991813: 100%| ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h ^v^h| 2/2 [00:02<00:00, >
EPOCH: 0, LOSS: 0.5779017142016517
EPOCH: 1, LOSS: 0.5527094161424018

So I wanted to know if its possible to flush/erase out the previous tqdm outputs from files(keep only the last description) thus mimicking the final output from when executed without nohup所以我想知道是否有可能从文件中刷新/擦除之前的 tqdm 输出(只保留最后的描述)从而模仿在没有 nohup 的情况下执行时的最终 output

No.不。

Please understand that under nohup you don't have a pty, a terminal, it just connects sys.stdout to the file nohup.out .请理解,在nohup下你没有 pty,一个终端,它只是将sys.stdout连接到文件nohup.out Normally tqdm does not exactly "erase", rather it prints a line of progress output followed by \r RETURN without \n NEWLINE.通常 tqdm 并不完全“擦除”,而是打印一行进度 output 后跟\r RETURN,不带\n NEWLINE。 Doing that repeatedly gives the visual effect of overwriting same line.重复这样做会产生覆盖同一行的视觉效果。 But many characters were output, and they all appear in the text output file.但是很多字符都是output,它们都出现在文本output文件中。

Your complaint is that "noisy" tqdm output interferes with other output, and you're hoping to see less noise.您的抱怨是“嘈杂的”tqdm output 干扰了其他 output,您希望看到更少的噪音。 To do that, either add an option to your app so it doesn't call tqdm at all, or ensure that your app writes its output to some other file descriptor.为此,请向您的应用程序添加一个选项,使其根本不调用 tqdm,或者确保您的应用程序将其 output 写入其他文件描述符。 Then when you cat that file you won't see any progress output.然后,当您cat该文件时,您将看不到任何进度 output。

Currently you have目前你有

from tqdm import tqdm

        ...
        for item in tqdm(items):
            do_stuff(item)

You can create your own def tqdm(...): wrapper which conditionally calls the "real" tqdm.您可以创建自己的def tqdm(...):有条件地调用“真实”tqdm 的包装器。 If a user-supplied option or isatty() indicate that we don't wish to see progress output, then your wrapper can choose to silently return instead of passing through to a tqdm call.如果用户提供的选项或isatty()表示我们不希望看到进度 output,那么您的包装器可以选择静默返回而不是传递给 tqdm 调用。

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

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