I am trying to learn Python multiprocessing.Pool
.
import numpy as np
from multiprocessing import Pool
def topla(sayı):
return sayı+2
def summ(number):
results=[]
array=np.linspace(0,number,20)
if __name__ == "__main__":
p = Pool(4)
simulation=p.map(topla, array)
results.append(simulation)
return results
sum_res=summ(8)
print(sum_res)
When you run this code, you will see an (1,1) array such as :
[[2.0, 2.4210526315789473, 2.8421052631578947, 3.263157894736842, 3.6842105263157894, 4.105263157894736, 4.526315789473684, 4.947368421052632, 5.368421052631579, 5.789473684210526, 6.2105263157894735, 6.631578947368421, 7.052631578947368, 7.473684210526315, 7.894736842105263, 8.31578947368421, 8.736842105263158, 9.157894736842104, 9.578947368421051, 10.0]]
However when you try to reach sum_res[0]
results in an out of index error:
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\BURGer\Desktop\eren\junk.py", line 26, in <module>
print(sum_res[0])
IndexError: list index out of range
Similar errors are constantly appearing unless I close the kernel. I am usng spyder, python 3.9.7
Do you know how to solve this problem? Thanks