Skip to content

mcmc_negfc_sampling requires if __name__ == '__main__': in order to run in a .py script #668

@IainHammond

Description

@IainHammond

This issue actually comes from emcee, rather than from VIP itself.

In summary, mcmc_negfc_sampling runs with multiprocessing (or without) with no problems in the test suite and in a Jupyter Notebook. If it is run within a python script, the following message appears and is trigger by the call to emcee.EnsembleSampler:

RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.

This occurs regardless of using forkserver, fork, or spawn. The script is launched multiple times and emcee.EnsembleSampler is consequently launched multiple times. The way around this is to place your script that contains a call to mcmc_negfc_sampling under if __name__ == '__main__': to ensure that the code block beneath it only runs when the script is executed directly.

Simply following the basic parallelization tutorial on the emcee docs (https://emcee.readthedocs.io/en/stable/tutorials/parallel/) will produce an identical error, as they don't warn of this problem.

Someone smarter than me will need to determine a way around this, or we could suggest a change to emcee itself. One option is to ditch multiprocessing of emcee entirely, and only pass nproc to PCA functions. Although, it is about 25-30% slower.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions