'tqdm skips line when one bar finishes with multithreading

When using tqdm with multithreading, tqdm seems to jump down a line and overwrite what was there when one thread finishes. It seems to snap back once all threads have finished, but I have some long running threads and the progress bars look pretty bad as it is.

enter image description here

I created an example program to be able to replicate the issue. I basically just stripped out all of the business logic and replaced it with sleeps.

from concurrent.futures import ThreadPoolExecutor
from tqdm.auto import tqdm
from time import sleep
from random import randrange


def myf(instance: int, name: str):
    rand_size = randrange(75, 150)
    total_lines = 0
    # Simulate getting file size
    # Yes there's probably a better way to get the line count, but this
    # was quick and dirty and works well enough. The sleep is just there
    # to slow it down for the example
    for _ in tqdm(
        iterable=range(rand_size),
        position=instance,
        desc=f'GETTING LINE COUNT: {name}',
        leave=False
    ):
        sleep(0.1)
        total_lines += 1
    # Simulate the processing
    for record in tqdm(
        iterable=range(rand_size),
        total=total_lines,
        position=instance,
        desc=name
    ):
        sleep(0.2)


def main():
    myf_args = []
    for i in range(10):
        myf_args.append({
            'instance': i,
            'name': f'Thread-{i}'
        })

    with ThreadPoolExecutor(max_workers=len(myf_args)) as executor:
        executor.map(lambda f: myf(**f), myf_args)


if __name__ == "__main__":
    main()

I'm looking for a way to keep the progress bars in place and looking neat as it's running so I can get a good idea of the progress of each thread. When googling the issue, all I can find are people having an issue where it prints a new line every iteration, which isn't really applicable here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source