Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple pbars with leave=None cause leftover stuff on console and wrong position of cursor #1434

Open
5 of 6 tasks
fireattack opened this issue Feb 22, 2023 · 3 comments
Open
5 of 6 tasks

Comments

@fireattack
Copy link

fireattack commented Feb 22, 2023

  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
  • I have visited the source website, and in particular
    read the known issues
  • I have searched through the issue tracker for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable: 4.64.1 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] win32

When manually controlling multiple pbars, there are chances that something stuff would be left on the console.

Furthermore, the cursor will NOT be moved back to the beginning, cause nexe print starts in the middle of the line.

STR:

from tqdm import tqdm
import time
import concurrent.futures
import random

NUM = 5
bars = []
total_chunks = NUM * 3

for i in range(NUM):
    bars.append(tqdm(total=1024, unit="B", unit_scale=True, leave=None, unit_divisor=1024, ncols=100, position=i))

def upload_chunk(chunk_no, total_chunks):
    bar = bars[chunk_no % NUM]

    size = int(1024*1024*10*random.random())
    if bar:
        bar.desc = f'chunk {chunk_no + 1}/{total_chunks}'
        bar.reset(total=size)
    def gen():
        offset = 0
        while True:
            if offset < size:
                update_chunk = 1024*1000
                time.sleep(0.1)
                yield 'something'
                offset += update_chunk
                if bar:
                    bar.update(update_chunk)
            else:
                break
    result = list(gen())
    return result

with concurrent.futures.ThreadPoolExecutor(max_workers=NUM) as ex:
    for i in range(0, total_chunks):
        ex.submit(upload_chunk, i, total_chunks)


for bar in bars:
    bar.close()

print('Done!')

Final result:

Windows Terminal - CMD:

image

Windows Terminal - PS7:

image

Ubuntu (remote):

image

@mjpieters
Copy link

I'm pretty sure this is the same issue as #1496. Could you try and see if my PR (#1502) fixes this issue for you? You can install the PR changes locally with:

pip install git+https://github.com/tqdm/tqdm.git@refs/pull/1502/merge

@fireattack
Copy link
Author

It definitely makes some difference, but I'm not sure if either is correct.

Since I use leave=None, it should be something like

Expected:

chunk 11/15: 9.77MB [00:01, 10.2MB/s]
Done!

4.66.1 release output:

chunk 11/15: 9.77MB [00:01, 10.2MB/s]
chunk 12/15: 5.86MB [00:00, 20.3MB/s]Done!

PR #1502 output:

chunk 11/15: 2.93MB [00:01, 2.77MB/s]
chunk 12/15: 9.77MB [00:01, 10.2MB/s]
chunk 13/15: 6.84MB [00:00, 8.88MB/s]
chunk 14/15: 4.88MB [00:00, 7.25MB/s]
chunk 15/15: 6.84MB [00:00, 10.2MB/s]
Done!

As you can see, it behaviors like leave=True.

@mjpieters
Copy link

mjpieters commented Aug 26, 2023

Thanks for trying out the PR!

I had not accounted properly for leave=None in the first revision of the PR. Could you try again? I believe that if you use the pip command again it'll install the newest version.

At any rate, running your script now outputs:

chunk 11/15: 2.93MB [00:01, 2.69MB/s]
Done!

for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants