Say I want to share a dictionary between processes. If I have defined a manager, what is the difference between instantiating a pool using manager.Pool() and multiprocessing.Pool()?
Ex: What is the difference between the two with statements in main_1 and main_2?
import multiprocessing as mp
import time
from random import random
def func(d):
    pid = mp.current_process().pid
    time.sleep(3 * random())
    d[pid] = 'I added this'
def main_1():
    print('Start of main 1')
    with mp.Manager() as manager:
        d = manager.dict()
        with manager.Pool() as pool:  # using manager
            pool.map(func, [d] * 4)
        print(d)
def main_2():
    print('Start of main 2')
    manager = mp.Manager()
    try:
        d = manager.dict()
        with mp.Pool() as pool:  # using multiprocessing
            pool.map(func, [d] * 4)
        print(d)
    finally:
        manager.shutdown()
if __name__ == '__main__':
    main_1()
    main_2()
Are all processes started after a Manager exists in the scope automatically served by it?
Both manager.Pool and multiprocessing.Pool in Python are used for creating a pool of worker processes to execute tasks in parallel. However, there are some differences between them.
multiprocessing.Pool is a built-in class provided by the Python multiprocessing module. It creates a pool of worker processes, where each process executes tasks independently. The tasks can be submitted to the pool as functions or methods to be executed in parallel.
On the other hand, manager.Pool is a class provided by the Python multiprocessing.managers module. It also creates a pool of worker processes like multiprocessing.Pool. However, manager.Pool is used for creating a pool of processes that can be shared among multiple Python processes. This means that the processes in the pool can be accessed and used by multiple Python processes, making it a good choice for distributed computing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With