0

I am having issues with trying to use global variables in a multiprocessing pool. I know there is quite a lot of content on this already, but I am struggling to implement the answers to my case.

I have a function that I want to use with pool.map. I want the input of this function to be a few global variables and also one other parameter that changes process-to-process. That is I want something that looks like:


if __name__ == '__main__':

   A = Global Variable Array
   B = Global Variable Array
   C = Actual Input Array For the Multiprocessing

   pool = multiprocessing.Pool()
   Res= pool.map(Function,Input = A,B,C)

In the above case only C varies from process-to-process. Currently my code only works if I import the global arrays from an external files within each process, which seems very wasteful.

1
  • "I want something that looks like" What stopy you from writing something that looks like this? What have you tried so far? Did you get an error? What do you intend Input = A, B, C to mean? Pool.map does not take a parameter Input, and Input = A, B, C passes only A to Input. Commented Jul 12, 2020 at 13:27

1 Answer 1

1

multiprocessing provides a couple of ways to share state between processes. Access to these objects may be synchronized by lock arg.

If lock is True (the default) then a new recursive lock object is created to synchronize access to the value. If lock is a Lock or RLock object then that will be used to synchronize access to the value. If lock is False then access to the returned object will not be automatically protected by a lock, so it will not necessarily be “process-safe”.

Example from the official documentation:

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print(num.value)
    print(arr[:])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.