Python Glossary Русский

Python 3.4+multiprocessingfunction

multiprocessing.set_forkserver_preload()

start methods

Sets the modules the forkserver server imports up front so that every new process gets them already loaded. It only matters with the forkserver start method.

Syntax

multiprocessing.set_forkserver_preload(module_names)

Call it before the first process or pool: the server starts once, and a late call silently changes nothing. A module that fails to import is skipped without a word by default, so a typo in the name goes unnoticed (3.15 added on_error='warn' or 'fail'). Under spawn and fork the call does nothing, and Windows has no forkserver at all. The gain shows with heavy modules such as numpy: the import happens once in the server instead of in every child.

Examples

import multiprocessing as mp
def job():
    pass
if __name__ == '__main__':
    if 'forkserver' in mp.get_all_start_methods():
        ctx = mp.get_context('forkserver')
        ctx.set_forkserver_preload(['json', 'no_such_module'])
        p = ctx.Process(target=job)
        p.start()
        p.join()
        print('exitcode', p.exitcode)  # → exitcode 0 — опечатку в имени никто не заметил
    else:
        print('exitcode 0')  # на Windows forkserver нет

See also

Open in the full glossarydocs.python.orgReport an inaccuracy