Shared numpy array with multithreading
<p>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.</p>

<pre><code>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)
</code></pre>

<p>You are using a <code>ProcessPoolExecutor</code>.  This will fork new processes for performing work.  These processes will not share memory, each instead getting a copy of the <code>distances</code> matrix.</p>

<p>Thus any changes to their <em>copy</em> will certainly not be reflected in the original process.</p>

<p>Try using a <code>ThreadPoolExecutor</code> instead.  </p>

<p><strong>NOTE:</strong> Globals are generally viewed with distaste ... pass the array into the function instead.</p>

python multithreading