How to "flush" tqdm progress bar explicitly?

Dims picture Dims · Aug 24, 2017 · Viewed 12.3k times · Source

I often see, that tqdm progress bar is broken by other print, like:

 93%|█████████▎| 28/30 [00:02<00:00, 13.44it/s]Subject S9
100%|██████████| 30/30 [00:02<00:00, 12.94it/s]
 93%|█████████▎| 28/30 [00:02<00:00, 11.49it/s]Pickling...
100%|██████████| 30/30 [00:02<00:00, 11.47it/s]

Here only 2 progress bars should be shown. Nevertheless succeeded, printing of some text interrupts progress bar at high percentage and the rest of it is printed out afterwards.

Is it possible to "flush" progress bar somehow?

I read, that tqdm prints to stderr by default and tried to flush it

sys.stderr.flush()

but this didn't helped.

All above is happened in PyCharm console simulation, so it can be related with this.

Answer

ZaydH picture ZaydH · Feb 3, 2019

Without seeing more of your code, it is not possible to say with certainty what is happening here. However, what follows is the most likely explanation.

By default, tqdm prints to stderr. Your statements Subject... and Pickling... are printing to stdout. By default those two streams are not in sync (I do not know if it is possible to sync them even).

If you want tqdm to be in sync with prints, you have the option to route tqdm to stdout instead of stderr. This is achieved via:

tqdm(xrange(50), file=sys.stdout)

You would then not need to flush stdout.