Python uses a dictionary between parallel processes

I want to share a dictionary between my processes as follows:

def f(y,x):
    y[x]=[x*x]                                                          

if __name__ == '__main__':
    pool = Pool(processes=4)
    inputs = range(10)
    y={}                             
    result = pool.map(f,y,inputs)

y returns {}. How can I make it work?

Thank,

+5
source share
1 answer

It looks like you are using a module multiprocessing. You did not say, and this is important information.

A function .map()in an instance multiprocessing.Pool()takes two arguments: a function and a sequence. The function will be called with sequential values ​​from the sequence. You are trying to convey a ysequence, and it will not work.

: ((y, x) for x in input) , , , - .

multiprocessing.Pool() , , , : , dict, .

, , . , :

import multiprocessing as mp

def f(x):
    return (x, x*x)

if __name__ == '__main__':
    pool = mp.Pool()
    inputs = range(10)
    result = dict(pool.map(f, inputs))

result : {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

, x*x x , ​​. . , f() , (key, x, p), x**p.

import multiprocessing as mp

def f(tup):
    key, x, p = tup  # unpack tuple into variables
    return (key, x**p)

if __name__ == '__main__':
    pool = mp.Pool()
    inputs = range(10)
    inputs = [("1**1", 1, 1), ("2**2", 2, 2), ("2**3", 2, 3), ("3**3", 3, 3)]
    result = dict(pool.map(f, inputs))

, , , zip() , , itertools.product.

+9

All Articles