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

contextis an optional argument in the constructor class multiprocessing.pool.Pool. The documentation says only:

contextcan be used to indicate the context used to start workflows. Typically, a pool is created using a function multiprocessing.Pool()or Pool()context object. In both cases, the context is set accordingly.

It does not specify what an "object-context" is, why a constructor class Poolneeds it, and what means that it is "set appropriately" in these scenarios.

+4
source share
1 answer

. :

  • spawn:

    python.
    Unix Windows. Windows.

  • :

    os.fork() Python. Unix. Unix.

  • forkserver

    forkserver, . , , . , os.fork(). .

    Unix, Unix-.


, set_start_method() if __name__ == '__main__' . :

import multiprocessing as mp

def 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()

get_context() . API, , .

import multiprocessing as mp

def 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()

!

+3

Source: https://habr.com/ru/post/1676542/


All Articles