rasbt / pyprind

PyPrind - Python Progress Indicator Utility
BSD 3-Clause "New" or "Revised" License
547 stars 65 forks source link

Flushing the stdout #40

Closed vmirly closed 7 years ago

vmirly commented 7 years ago

I have seen when I am printing some messages using print() statement prior to creating object of pyprind.ProgBar, the process bar gets split into separate parts like example below:

0%                          100%
Dataset: muct-subjects.txt   Task: genuine  VGG: orig/
Number of Subjects:  276
[##############################] | ETA: 00:00:00
Total time elapsed: 00:00:06

In order to fix this, I had to manually flush the stdout with:

import sys
sys.stdout.flush()

and that will fix it.

Just as a suggestion, it will be helpful to put the sys.stdout.flush() in the constructor to take care of it.

rasbt commented 7 years ago

thanks for the note Vahid. Quick question, which version of pyprind are you using that had this issue?

vmirly commented 7 years ago

I installed the newest version using pip.

import pyprind
pyprind.__version__
'2.10.0'

I am using python 3.5, from Anaconda distribution.

rasbt commented 7 years ago

Thanks, fixed that now.

vmirly commented 7 years ago

Thank youSebastian,

however, there is still a problem (in version 2.11.0)

0% [##                            ] 100% | ETA: 00:00:02
2.11.0
Data: pert_IFACE/  Output: /Users/vahid/tahq/vggdescriptors/results.vgg-matchscores/pertGenderIFACE-genine-cross
0% [##############################] 100% | ETA: 00:00:00

The code is given below. I am printing some stuff before creating ProgBar object, but it gets mixed with progress-bar:

print(pyprind.__version__)
## Genuine Scores
for vggpath in ['pert_IFACE/', 'pert_ROC/']:
    path = vggmain_path + vggpath
    output_fname = vggmain_path + 'results.vgg-matchscores/' + outputnames[vggpath] + task
    print('Data: {}  Output: {}'.format(vggpath, output_fname))
    #sys.stdout.flush()
    subj_dict = get_subjdict(path)
    subj_dict_orig = get_subjdict(vggmain_path + 'orig/')
    n_subjects = let(list(subj_dict.keys()))
    pbar = pyprind.ProgBar(n_subjects)
rasbt commented 7 years ago

Thanks for the feedback. That's interesting. So I guess either

a) uncommenting

#sys.stdout.flush()

or b)

print('Data: {}  Output: {}'.format(vggpath, output_fname), flush=True)

would solve the issue. Right now, pyprind is only flushing the output stream its actually using (by default, it's stderr since that what most people prefer so that they can redirect stdout to files without cluttering it up with progress bars).

In any case, flushing both stderr and stdout prior to initializing the progress bar seems reasonable to me, will make that change