Add hint of duration for each iteration in tqdm

103 views Asked by At

I have a list of tasks that each take a different amount of time. Let's say, I have 3 tasks, with durations close to 1x, 5x, 10*x. My tqdm code is something like:

from tqdm import tqdm

def create_task(n):
    def fib(x):
        if x == 1 or x == 0:
            return 1
        return fib(x - 1) + fib(x - 2)
    return lambda: fib(n)

n = 1
tasks = [create_task(n), create_task(5*n), create_task(10*n)]
for task in tqdm(tasks):
    task.run()

The problem is that tqdm thinks each iteration takes the same amount of time. As the first takes approximately 1/10 of the time, the ETA is unreliable.

My question: is it possible to somehow add a hint to tqdm to inform how much each iteration takes compared to the first? Something like informing the duration weights of each iteration...

Thanks!

1

There are 1 answers

1
Jun-Jie Huang On

If "create_task" can be modified, the "inspect" library can help.

import time
from tqdm import tqdm
from inspect import currentframe,getargvalues

def create_task(n):
    '''
    On my computer, "tqdm" cannot handle the Fibonacci sequence.
    '''
    # def fib(x):
    #     if x == 1 or x == 0:
    #         return 1
    #     return fib(x - 1) + fib(x - 2)
    def func(x):
        time.sleep(x)
    return lambda: func(n), getargvalues(currentframe()).locals['n']

n = 1
tasks = [create_task(n), create_task(5*n), create_task(10*n)]

bar = tqdm(enumerate(tasks), total=len(tasks), desc=f"Takes 1 times as long as the previous loop.")
for idx, (task, curr_n) in bar:
    bar.set_description(f"Takes {curr_n/n:.2f} times as long as the first loop.")
    task()