MDAnalysis / mdanalysis

MDAnalysis is a Python library to analyze molecular dynamics simulations.
https://mdanalysis.org
Other
1.26k stars 639 forks source link

AnalysisBase ProgressBar won't display under Jupyter when multiprocessing #3335

Open mnmelo opened 3 years ago

mnmelo commented 3 years ago

When using multiprocessing for parallelization with Jupyter, having a forked child run an analysis (or a chunk thereof) and print the progress bar fails to show the bar (nothing is shown but execution does progress in parallel).

The server console complains once per iteration that [IPKernelApp] WARNING | WARNING: attempted to send message from fork plus the actual JSON progress bar update message.

This has been reported for other, cases. The latter link provides a workaround that solves it for me: to make the child(ren) that do the progress bar outputting print and flush something (can be a blank space) immediately upon initialization. This is simplest accomplished using an initializer function to multiprocess.Pool:

def display_hack():
    sys.stdout.write(' ')
    sys.stdout.flush()

pool = multiprocess.Pool(initializer=display_hack)
...

There is possibly a less hackish solution if some ProgressBar internals are reworked. The links I post mention the use of tqdm.contrib.concurrent (which seems to wrap around parallelization). Allowing AnalysisBase to pass kwargs to the ProgressBar initialization (requested in #3190) may also offer other ways around this.

appassionate commented 2 years ago

Hi! Maybe similar to you, I have another hack way to "squeeze out" tqdm ProgressBar. Differently,it will conbined with Analysis class in Pool. It will be applied in a customized AnalysisMethod which you might designed.

class AnalysisMethod(AnalysisBase):

    def run(start,step,stop,verbose=False):

        # a weird print to ​"squezze out" tqdm based ProgressBar
        if verbose == True:
            print(" ", end='')