In the code below I am trying to acsess the dictionaries in the list S
:
import multiprocessing
from multiprocessing import Pool, cpu_count
def test(s):
s["key"]+=1
print( s )
S = [ { "key": 0 } for i in range(2) ]
pool = Pool(processes = 2 )
tasks = [ pool.apply_async( test, args = ( S[i], ) ) for i in range(2) ]
for t in tasks:
t.get()
print(S)
As usually it happens in python I expect S[i]
to be passed by reference. But what I get as output is: [{'key': 0}, {'key': 0}]
instead of [{'key': 1}, {'key': 1}]
. Why is it so? How can I acsess the elements in the dictionaries in parallel?