Skip to content Skip to sidebar Skip to footer

Python Multiprocessing - Independently Processing For Each Key-value Pair In The Dictionary

I have a dictionary that looks like this: sampleData = {'x1': [1,2,3], 'x2': [4,5,6], 'x3': [7,8,9]} I need to do some calculation for each key and value pair by passing data to a

Solution 1:

Try something along the lines

from multiprocessing import Pool

def pair_black_box(data):
    key, values = data
    res = []
    for i in range(0, len(values)):
        for j in range(i, len(values)):
            if(i != j):
               res.append(blackBoxFunction(values[i], values[j]))
    return key, res

p = Pool(3)
finalValue = dict(p.map(pair_black_box, sampleData.items()))

Post a Comment for "Python Multiprocessing - Independently Processing For Each Key-value Pair In The Dictionary"