I'm trying to access a global dict from different threads. A complete example is below:
results = {0: 'pass'}
def checkResult(thread_num, result_map):
while(True):
results[thread_num] = 'fail'
print('Thread results : '+str(results))
time.sleep(5)
multiprocessing.Process(target = checkResult, args=(1, results)).start()
multiprocessing.Process(target = checkResult, args=(2, results)).start()
while(True):
print('Main results: '+str(results))
time.sleep(3)
The "main results" only sees thread 0, and the "thread results" only modify the dict as per their own thread's results:
Thread results : {0: 'pass', 1: 'fail'}
Main results: {0: 'pass'}
Thread results : {0: 'pass', 2: 'fail'}
Main results: {0: 'pass'}
Thread results : {0: 'pass', 1: 'fail'}
Thread results : {0: 'pass', 2: 'fail'}
Main results: {0: 'pass'}
^C
It seems that the "results" map is being passed-by-value instead of passed-by-reference. Is there any way to have the threads refer to the original global map instead of having each thread use it's own copy?
I know that I should be using locking to avoid issues where one thread overwrites the changes of another (and I plan to do so in the actual solution). For now though I'm just trying to have all the threads working with a common results dictionary.