I need your help with multiprocessing to update global dict data. I use two functions, the first function is to do multiprocessing in the for loop. the second function is to update global dict data. after multiprocessing, all the data was not updated.
Here is my sample code:
import concurrent.futures
from multiprocessing import Process
from multiprocessing import Manager
list_key = ['A','B','C','D','E','F','G','H','I','K']
# first function. increase the value in Dict_value
def first(x,y):
print('Call Increase Value Function')
print(x)
print(y[x])
y[x] += 1
print(y[x])
print('Finished Increase Value')
# second function. square the value in the Dict_value
def second(x,y):
print('Call sqrt_dict_value function')
print(y)
for i in x:
y[i] = y[i]* y[i]
print(y[i])
print('Finished sqrt_dict_value')
def main():
# declare the list and dict
with Manager() as manager :
dict_value = manager.dict()
list_key = manager.list()
list_key = ['A','B','C','D','E','F','G','H','I','K']
dict_value = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'K':9}
# multiprocessing for first function.
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
process = executor.submit(first,list_key,dict_value)
# square the value in the dict after increase value in the first function.
second(list_key,dict_value)
if __name__ == '__main__':
main()
My expected result is: 1 4 9 16 25 36 49 64 81 100