How to use multiple parameters in multiprocessing Pool?

Following is the function I want to call using multiprocessing:

def Y_X_range(ranges, dim, Ymax, Xmax):
    print('len: ', ranges, dim)
    for i in enumerate(ranges):
        if i[0] == 0 or i[0] == 2:
            Yi = 0
            while (Yi * dim < Ymax):
                ranges[i[0]] += [-1]
                Yi = Yi + 1
        elif i[0] == 1 or i[0] == 3:
            Xi = 0
            while (Xi * dim < Xmax):
                ranges[i[0]] += [-1]
                Xi = Xi + 1
    return ranges[0]

If I try to call the function using codes:

ranges = [[], [], [], []]
p = multiprocessing. Pool()
result = p.map(Y_X_range, ranges, dim, Ymax, Xmax)

Then I get an error message stating:

result = p.map(Y_X_range, ranges, dim, Ymax, Xmax) 
TypeError: map() takes from 3 to 4 positional arguments but 6 were given

Can anyone tell me how can I pass values for all the parameters in Y_X_range(ranges, dim, Ymax, Xmax)?

Since you only call Y_X_range once with one set of arguments, I don’t know why you are using Pool and map. But for map, read this doc entry

and the entry below for starmap.

1 Like

@tjreedy My intention was to use 4 logical CPUs individually for ranges[0], ranges[1], ranges[2] and ranges[3].

If possible, can you suggest something?

Got the solution. So, I am sharing here:

ranges = []

def Y_X_range(i, dim, Ymax, Xmax):
    ranges_i = []
    if i == 0 or i == 2:
        Yi = 0
        while (Yi * dim < Ymax):
            ranges_i.append(-1)
            Yi = Yi + 1
    else:
        Xi = 0
        while (Xi * dim < Xmax):
            ranges_i.append(-1)
            Xi = Xi + 1
    return ranges_i

def get_range(ranges_i):
    global ranges
    ranges.append(ranges_i)
global ranges
p = multiprocessing.Pool(4)
for i in range(4):
    p.apply_async(Y_X_range, args=(i, dim, Ymax, Xmax), callback=get_range)
p.close()
p.join()