Reputation: 195
I have a simple function to use on an array, but when I use multiprocessing.Pool with the function and array, I get the error message "tuple index out of range". How do I fix it?
import numpy as np
import multiprocessing
var1 = 4
array = np.zeros((var1, var1))
for i in range(np.size(array,1)):
array[i,0] = 1
def function(array):
for i in range(np.size(array,1)):
for t in range( np.size(array,0) - 1):
array[i,t+1] = array[i,t] + 1
return(array)
from multiprocessing import Pool
if __name__ == '__main__':
pool = Pool(processes=2)
print(pool.map(function, array))
I expect to see a 4x4 array where each row is: 1 2 3 4.
Upvotes: 2
Views: 737
Reputation: 31584
If not use pool.map
, the array
which the function get is:
[[1. 0. 0. 0.]
[1. 0. 0. 0.]
[1. 0. 0. 0.]
[1. 0. 0. 0.]]
If use pool.map
, the array
which the function get is:
[1. 0. 0. 0.]
So, you should not use the same logic to handle array
in function
just like not use pool.map
.
map(func, iterable[, chunksize=None])
You need to use like follows:
import numpy as np
import multiprocessing
var1 = 4
array = np.zeros((var1, var1))
for i in range(np.size(array,1)):
array[i,0] = 1
def function(array):
for i in range(np.size(array) - 1):
array[i+1] = array[i] + 1
return(array)
from multiprocessing import Pool
if __name__ == '__main__':
pool = Pool(processes=2)
print(pool.map(function, array))
Upvotes: 1