0

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.

Chris Fell
  • 53
  • 4
  • You are using multiple processes, not threads. – syntonym Aug 23 '16 at 11:48
  • Check [this](http://stackoverflow.com/questions/10797998/is-it-possible-to-multiprocess-a-function-that-returns-something-in-python) answer, it explains how to pass results back from a subprocess. – FujiApple Aug 23 '16 at 12:24

1 Answers1

1

What you need is parallel access to a global object which can be done by using threads instead of processes. All threads share the same adressspace which means, they have access to the same refereces. (also instead of using a dict with integer-keys, you could use a list)

If you're using unique "thread numbers" you actually don't need to worry about race-conditions because every thread writes to it`s own index.

Something like this should do the trick:

import _thread, time

results = {0 : 'pass'}

def checkResult(thread_num):
    while True:
        results[thread_num] = 'fail'
        print('Thread results: ' + str(results))
        time.sleep(3)

_thread.start_new_thread(checkResult, (1,))
_thread.start_new_thread(checkResult, (2,))

while True:
    print('Main results: ' + str(results))
    time.sleep(3)

(this might help you)

RobinW
  • 292
  • 4
  • 16
  • Ah, that works perfectly! I didn't even realise the difference between threading and multi-processing, but it makes sense. (and the "thread numbers" thing is part of my simplified-beyond-recognition example, but thanks for the tip!) – Chris Fell Aug 24 '16 at 09:29