brt
brt

Reputation: 47

problem with shared memory in python not getting into function

So I have arg parse where it calls the file.py in it, I have a parent function that makes children using multiprocessing process I have shared memory in the beginning, but it doesn't get access.

This is not really the code, too many lines but I think this is not a coding problem

Seed of my problem, For some reason, even though I have an array of shared memory this one cannot be found in the function temp giving the error NameError: name 'arr1' is not defined on line arr1.value[0] += 1

p is what makes mores processes.

I think it may be because of the reading of the file by the argparse. Any ideas?

Upvotes: 0

Views: 500

Answers (1)

Frank Yellin
Frank Yellin

Reputation: 11283

The examples in the documentation show arr1 created in the main thread and then passed as an argument to each of these processes. You don't want it created in each process.

So you would want something like:

imports.....

function temp(listFile, a, c, l, arr1):
   arr1[0] += 1

function main1(files = args.f, a = args.a, c = args.c, l = args.l, p = args.p):
    arr1 = Array("i", args.p)
    newP = Process(target=temp, args = (listFile, a, c, l, arr1))
    newP.start()
    newP.join() 
    # arr1[0] should now be 1.

One commenter notes that they prefer the creation of shared memory at top level. In that case you should move the arr1 = ... to be under the if __name__ == '__main__'. The important thing is that the shared memory be created once by the main thread.

Upvotes: 1

Related Questions