[Python] When using the multiprocessing pool without a context manager, is it necessary to...

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    The python docs seem to suggest that it's required to call both close() and terminate() when using multiprocessing.pool.Pool without a context manager.


    Warning - multiprocessing.pool objects have internal resources that need to be properly managed (like any other resource) by using the pool as a context manager or by calling close() and terminate() manually. Failure to do this can lead to the process hanging on finalization.
    ​

    However, calling both close() and terminate() seems redundant. The definitions of the functions in the docs say that close() or terminate() are called before join(), and the definitions of close() and terminate() suggest that the actions of close() would be nullified by terminate().


    close()

    • Prevents any more tasks from being submitted to the pool. Once all the tasks have been completed the worker processes will exit.

    terminate()

    • Stops the worker processes immediately without completing outstanding work. When the pool object is garbage collected terminate() will be called immediately.

    join()

    • Wait for the worker processes to exit. One must call close() or terminate() before using join().
    ​

    Is it necessary or advisable to call both close() and terminate()?

    Continue reading...

Compartilhe esta Página