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

Do an automatic stream flush before rendering a progress bar #1177

Closed
nickion opened this issue Jun 3, 2021 · 8 comments · Fixed by #1180
Closed

Do an automatic stream flush before rendering a progress bar #1177

nickion opened this issue Jun 3, 2021 · 8 comments · Fixed by #1180
Assignees
Labels
p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature to-fix ⌛ In progress
Projects
Milestone

Comments

@nickion
Copy link

nickion commented Jun 3, 2021

4.60.0 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] win32
Running Python in Spyder

While tqdm is great for the negligible effort required to have a progress bar, I've found it necessary to add a sys.stdout.flush() call before usage to avoid possible interleaved output. This taints the code and reduces the ease of use. e.g. below, where although the 1 was printed before the bar was started, the output was interleaved.

1%|          | 14/2408 [00:00<00:17, 135.41it/s]1
11%|█         | 263/2408 [00:02<00:17, 123.80it/s]

An option to have tqdm automatically flush the output stream it's about to use would be a huge benefit.

@casperdcl
Copy link
Sponsor Member

interesting... is this consistently reproducible?

@casperdcl casperdcl added need-feedback 📢 We need your response (question) p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature labels Jun 6, 2021
@nickion
Copy link
Author

nickion commented Jun 7, 2021

Yes, every time. A flush always resolves.

I found a similar observation about buffering from a few years back. spyder-ide/spyder#2860
Their conclusion was that it's related to Qt and so they closed as not a Spyder bug. So it would be a tqdm enhancement that perhaps shouldn't be necessary but would be very useful.

I notice there's callback support in tqdm, but wasn't sure if tqdm itself has callbacks for the start, end and during processing. Given a start callback then a workaround might be to attach a function to flush, but a native feature would be ideal.

@casperdcl casperdcl self-assigned this Jun 7, 2021
@casperdcl casperdcl added this to the Non-breaking milestone Jun 7, 2021
@casperdcl casperdcl added to-fix ⌛ In progress and removed need-feedback 📢 We need your response (question) labels Jun 7, 2021
@casperdcl casperdcl added this to Next Release in Casper Jun 12, 2021
casperdcl added a commit that referenced this issue Jun 15, 2021
@casperdcl
Copy link
Sponsor Member

does the current devel fix it for you? (pip install "git+https://github.com/tqdm/tqdm@devel#egg=tqdm")

@nickion
Copy link
Author

nickion commented Jun 29, 2021

Sorry for the delayed feedback, and unfortunately not, a sys.stdout.flush() is still required before a call to tqdm.tqdm()

@casperdcl
Copy link
Sponsor Member

You mean sys.stderr.flush() is not enough? Even though tqdm writes to stderr you need to flush stdout first?

@nickion
Copy link
Author

nickion commented Jul 1, 2021

You mean sys.stderr.flush() is not enough? Even though tqdm writes to stderr you need to flush stdout first?

I didn't realise that tqdm is using stderr, but would explain the issue as there are then two independently buffered streams both writing to the same destination. stderr is traditionally used for errors and if there's an option to use stdout instead that could be an alternative to flushing, but to be safe, I'd probably opt simply for tqdm to flush both stderr and stdout when starting a new progress tracker.

casperdcl added a commit that referenced this issue Jul 4, 2021
@casperdcl
Copy link
Sponsor Member

casperdcl commented Jul 4, 2021

can you try devel again now?

@nickion
Copy link
Author

nickion commented Jul 5, 2021

Thanks, the current devel version now resolves this.

Casper automation moved this from Next Release to Done Jul 6, 2021
casperdcl added a commit that referenced this issue Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature to-fix ⌛ In progress
Projects
Casper
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants