I have a numpy array(matrix), which I want to fill with calculated values in asynchronously. As a result, I want to have matrix distances with calculated values, but at the end I receive matrix filled with default(-1) value. I understand, that something wrong with sharing distances between threads, but I can't figure out what's exactly wrong.
import numpy as np
import concurrent.futures
data = range(1, 10)
amount = len(data)
default = -1
distances = np.full((amount, amount), default, dtype=np.float32)
def calculate_distance(i, j):
global distances
if i == j:
distances[i][j] = 0
else:
calculated = data[i] + data[j] #doesn't matter how is this calculated
distances[i][j] = calculated
distances[j][i] = calculated
with concurrent.futures.ProcessPoolExecutor() as executor:
for i in range(0, amount):
for j in range(i, amount):
future = executor.submit(calculate_distance, i, j)
result = future.result()
executor.shutdown(True)
print(distances)
distances(actually of the memory in general). When these processes modifydistances, they modify their own copy. This is a simplification, as copy-on-write may be involved, but the end result is the same.ProcessPoolExecutor, you need to include aif __name__ == "__main__"guard, otherwise your code won't work on all platforms. Second, you wait for each task to finish before submitting the next one, so the tasks are executed seuqentially. And third, using threads generally won't allow you to get more processor time for Python code due to the Global Interpreter Lock.