What is the meaning of `context` argument in `multiprocessing.pool.Pool`? What is the meaning of `context` argument in `multiprocessing.pool.Pool`? python-3.x python-3.x

What is the meaning of `context` argument in `multiprocessing.pool.Pool`?


Depending on the platform, multiprocessing supports three ways to start a process. These start methods are:

  • spawn:

    The parent process starts a fresh python interpreter process.
    Available on Unix and Windows. The default on Windows.

  • fork:

    The parent process uses os.fork() to fork the Python interpreter.Available on Unix only. The default on Unix.

  • forkserver

    When the program starts and selects the forkserver start method, a server process is started. From then on, whenever a new process is needed, the parent process connects to the server and requests that it fork a new process. The fork server process is single threaded so it is safe for it to use os.fork(). No unnecessary resources are inherited.

    Available on Unix platforms which support passing file descriptors over Unix pipes.


To select a start method you use the set_start_method() in the if __name__ == '__main__' clause of the main module. For example:

import multiprocessing as mpdef foo(q):    q.put('hello')if __name__ == '__main__':    mp.set_start_method('spawn')    q = mp.Queue()    p = mp.Process(target=foo, args=(q,))    p.start()    print(q.get())    p.join()

Alternatively, you can use get_context() to obtain a context object. Context objects have the same API as the multiprocessing module, and allow one to use multiple start methods in the same program.

import multiprocessing as mpdef foo(q):    q.put('hello')if __name__ == '__main__':    ctx = mp.get_context('spawn')    q = ctx.Queue()    p = ctx.Process(target=foo, args=(q,))    p.start()    print(q.get())    p.join()

This is where the context object came from!