I have the following example code:
def my_function_caller():
samples = []
for t in range(2):
samples.append(my_function(t))
return samples
def my_function(t):
results = []
if __name__ == '__main__':
pool = Pool()
results = pool.map(task, range(5))
pool.close()
pool.join()
A = results[0]
return A
def task(k):
time.sleep(1)
result = k
return result
When I call my_function(t), I get the following error:
A = results[0]
IndexError: list index out of range
I expected pool.close() and pool.join() to make the program wait for all processes to finish so that I could then use the jointly computed result "results" afterwards. How can I force the program to wait or more generally, how can I directly use "results" in the function "my_function"?
EDIT: To recreate the error: This is the complete code that I am running (simply copied and pasted). The python file called main.py is located in a standard Python project and I am using Windows.
from multiprocessing import Pool
import time
def my_function_caller():
samples = []
for t in range(2):
samples.append(my_function(t))
return samples
def my_function(t):
results = []
if __name__ == '__main__':
pool = Pool()
results = pool.map(task, range(5))
pool.close()
pool.join()
A = results[0]
return A
def task(k):
time.sleep(1)
result = k
return result
a = my_function_caller()
Maybe, as additional information, I get the error message
A = results[0]
IndexError: list index out of range
several times, not just once.