the following code
import multiprocessing as mp
from multiprocessing.managers import SyncManager
n_cores = mp.cpu_count()
def parallel_fn(job_n, cache):
cache['job_b'] = job_n
return job_n
if __name__=="__main__":
with SyncManager() as manager:
shared_cache = manager.dict()
args = list(zip(range(n_cores), shared_cache))
with mp.Pool(n_cores) as pool:
result = pool.starmap(parallel_fn, args)
print(result)
print(shared_cache)
returns
16
Shared dict before: {}
Pool return: []
Shared dict after: {}
I was expecting 16 values in return from the pool and 16 values in the shared dictionary, but both are empty, anyone can help me out?
Multiprocessing is a red herring in this case. If you print
argsafter defining it, you'll see an empty list. You'll want to fix thezipline as follows to create a list of tuples.zipreturns the shorter of the two (or more) items. In this case, you have arangeobject of length 16 and aProxyDictobjectof length 0 (it's empty to start). As a small example, check out:list(zip([1, 2], dict()))which returns a list of length 0.Also, I'm guessing that you wanted to put
job_nas the name in the cache dictionary.On my 8-core machine, the output is: