tqdm 在较大循环内有间隔
tqdm with intervals inside larger loop
我有一个外循环
for i in itertools.count():
并且每隔一段时间就会发生一些事件:
if i % log_interval == 0:
perform_logging()
if i % save_interval == 0:
save()
# etc.
我想使用 tqdm 跟踪下一个 log/save/etc 的进度。
理想情况下,tqdm 会同时打印多个进度条,a la
最好的方法是什么?谢谢!
像这样:
from __future__ import division, print_function
import itertools
from math import ceil
from tqdm import tqdm
from time import sleep
total = 987
log_interval = 2
save_interval = 9
with tqdm(total=total, desc="overall") as tOverall:
with tqdm(total=ceil(total / log_interval), unit="log") as tLog:
with tqdm(total=ceil(total / save_interval), unit="save") as tSave:
for i in itertools.count(0, 1):
sleep(0.01)
if i % log_interval == 0:
perform_logging()
tLog.update()
if i % save_interval == 0:
save()
tSave.update()
if i + 1 == total:
break
tOverall.update()
print('\n')
您的版本存在一个小问题,进度条在每次迭代后都没有重新启动。我还希望 i
无限增加。这是我想出的版本:
from __future__ import division, print_function
import itertools
from math import ceil
from tqdm import tqdm
from time import sleep
total = 987
log_interval = 2
save_interval = 9
for i in itertools.count():
if i % log_interval == 0:
tLog = tqdm(total=ceil(total / log_interval), unit="log")
if i % save_interval == 0:
tSave = tqdm(total=ceil(total / save_interval), unit="save")
# do work
if i % log_interval == 0:
tLog.update()
if i % save_interval == 0:
tSave.update()
打印('\n')
我有一个外循环
for i in itertools.count():
并且每隔一段时间就会发生一些事件:
if i % log_interval == 0:
perform_logging()
if i % save_interval == 0:
save()
# etc.
我想使用 tqdm 跟踪下一个 log/save/etc 的进度。 理想情况下,tqdm 会同时打印多个进度条,a la
最好的方法是什么?谢谢!
像这样:
from __future__ import division, print_function
import itertools
from math import ceil
from tqdm import tqdm
from time import sleep
total = 987
log_interval = 2
save_interval = 9
with tqdm(total=total, desc="overall") as tOverall:
with tqdm(total=ceil(total / log_interval), unit="log") as tLog:
with tqdm(total=ceil(total / save_interval), unit="save") as tSave:
for i in itertools.count(0, 1):
sleep(0.01)
if i % log_interval == 0:
perform_logging()
tLog.update()
if i % save_interval == 0:
save()
tSave.update()
if i + 1 == total:
break
tOverall.update()
print('\n')
您的版本存在一个小问题,进度条在每次迭代后都没有重新启动。我还希望 i
无限增加。这是我想出的版本:
from __future__ import division, print_function
import itertools
from math import ceil
from tqdm import tqdm
from time import sleep
total = 987
log_interval = 2
save_interval = 9
for i in itertools.count():
if i % log_interval == 0:
tLog = tqdm(total=ceil(total / log_interval), unit="log")
if i % save_interval == 0:
tSave = tqdm(total=ceil(total / save_interval), unit="save")
# do work
if i % log_interval == 0:
tLog.update()
if i % save_interval == 0:
tSave.update()
打印('\n')