简体   繁体   English

在调用带有 tqdm 进度条的函数之前打印语句会导致在 tddm 之后打印打印语句

[英]Print statement just before calling a function with a tqdm progress bar results in the print statement being printed after tddm

When working with tqdm for a project, I've noticed that when I use a print statement just before I call a function that has a tqdm progress bar, the print statement is printed somewhere in between the progress bars.在为项目使用 tqdm 时,我注意到当我在调用具有 tqdm 进度条的函数之前使用打印语句时,打印语句打印在进度条之间的某处。

I've noticed that this behaviour only occurs in ipython notebooks, not when run in terminal.我注意到这种行为只发生在 ipython 笔记本中,而不是在终端中运行时。

ipython 中的示例输出

终端中的示例输出

from tqdm import tqdm

def long_progress():
    for _ in tqdm(range(100)):
        time.sleep(.05)

for i in range(5):
    print("Show parameters of the progress")
    long_progress()

This behaviour doesn't break anything, I'm just really curious why it is happening.这种行为不会破坏任何东西,我只是很好奇为什么会发生这种情况。

I've also noticed that when I use time.sleep(0.2) before I call tqdm it's resolved, so it seems like some timing issue.我还注意到,当我在调用 tqdm 之前使用 time.sleep time.sleep(0.2) ,它已解决,所以这似乎是一些时间问题。

Edit: Changed print statement to reflect my goal more precisely编辑:更改了打印语句以更准确地反映我的目标

Define the print statement outside the loop ie在循环外定义打印语句,即

print("Lets start long progress")
for i in range(5):
    long_progress()

This will call the print before the loop execution.这将在循环执行之前调用打印。

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

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