Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.freeze_support()

start methods

Support for multiprocessing in a program frozen into an executable (PyInstaller, cx_Freeze). Call it as the first line under if __name__ == '__main__'.

Syntax

multiprocessing.freeze_support()

Without it a frozen exe under spawn (on Windows) launches itself as the child and runs main again, giving an avalanche of processes or a RuntimeError instead of work. When run normally with python, and under fork and forkserver, the call does nothing, so it is safe to keep it always. It must come right after the __main__ check, before any other code.

Examples

import multiprocessing as mp
import sys
def job():
    pass
if __name__ == '__main__':
    mp.freeze_support()
    print(getattr(sys, 'frozen', False))  # → False — не exe, вызов ничего не сделал
    p = mp.get_context('spawn').Process(target=job)
    p.start()
    p.join()
    print(p.exitcode)  # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy