I have created a class where all the functions work and can be put into different processes and edited. All of the functions work except the add value function. This is my class:
class data(object):
def __init__(self):
Manager = multiprocessing.Manager()
self.lock = multiprocessing.Lock()
self.changeAmt = 0
self.jump = multiprocessing.Value(ctypes.c_float, 0.02)
self.has_best = multiprocessing.Value(ctypes.c_bool, False)
self.best_list = Manager.list()
self.command_original = 'start sequence'
self.command = multiprocessing.Value(ctypes.c_wchar_p, self.command_original)
self.get_best = multiprocessing.Value(ctypes.c_bool, False)
self.done = multiprocessing.Value(ctypes.c_bool, False)
def get(self, name, model):
self.model = model
if model != 'Array':
with multiprocessing.Lock():
exec('self.now = self.'+name)
return self.now.value
if self.model == 'Array':
exec('self.now = self.'+name)
return self.now
def edit(self, name, model, changeAmt):
self.changeAmt = changeAmt
if model != 'Array':
with multiprocessing.Lock():
exec('self.'+name+'.value = self.changeAmt')
if model == 'Array':
for i in range(len(changeAmt)):
exec('self.'+name+'.append(changeAmt[i])')
def addValue(self, name, value):
self.now = value
exec('self.'+name+' = self.now')
Which can then be used correctly with these lines of code in the main function:
ult = data()
p1 = Process(target = try2, args=(ult,))
p1.start()
p2 = Process(target = nextTry, args=(ult,))
p2.start()
p1.join()
p2.join()
When I go to use the addValue function in my code, the first process works great and can return the value when the get function is called. However, when I go to call the get function for the second process it fails. One of my ideas on a solution is to use the multiprocessing Queue method and have both processes add the value. Although, I have a feeling even if both functions have added the value it will not change if one of them changes the value. Is what I am trying to accomplish do-able, or should I just try to initialize all of my variables from the start?
Advertisement
Answer
I found that the only way to do this is to use a multiprocessing dictionary, I have posted the working example of an initializer and creator below.
def __init__(self):
Manager = multiprocessing.Manager()
self.allDict = Manager.dict()
self.allDict['status'] = 'nothing'
self.allDict['done'] = False
self.allDict['changeAmt'] = 0
self.allDict['command'] = 'start sequence'
self.allDict['get_best'] = False
then if you wanted to add a regular value:
def addValue(self, name, value):
self.allDict[name] = value
This honestly was probably one of the original reasons for the manager dictionary. Anyways, I hope if anyone needs this information and stumbles across this post it helps.