I'm trying to use a dictionary in my multiprocessing code. The problem is when I changed the value of one key in the process, the changes just affect only that process. Apparently, variable arguments of the process are a copy of that variable. I tried to use dict method but it does not work too. What is my mistake?
import time
from multiprocessing import Process, Manager
def f1(list1, set1):
list1.append(3)
set1['func']='f1'
print('f1 dic',set1)
print('f1 list',list1)
while True:
time.sleep(3)
print('f1 dic',set1)
print('f1 list',list1)
def f2(list1):
list1.append(4)
settings_dic['func']='f2'
print('f2 dic',settings_dic)
print('f2 list',list1)
while True:
time.sleep(3)
print('f2 dic',settings_dic)
print('f2 list',list1)
if __name__ == '__main__':
# list1 = Manager().dict()
settings_dic = Manager().dict()
Global = Manager().Namespace()
list1 = [1,2]
settings_dic = {
'mode' : 'master',
'logger' : 'True',
'func' : ''
}
p_video_get = Process(target=f1, args=(list1,settings_dic,))
p_video_get.daemon = True
p_video_get.start()
p_packetTrasnmission = Process(target=f2, args=(list1,))
p_packetTrasnmission.daemon = True
p_packetTrasnmission.start()
list1.append(5)
settings_dic['func'] = 'main'
print('main dic',settings_dic)
print('main list',list1)
while True:
time.sleep(3)
print('main dic',settings_dic)
print('main list',list1)
settings_dicis a regular dictionary not aManager().dict()but you instead re-assign a regular dict to the same variable