Multiprocessing - Shared Array
Question: So I'm trying to implement multiprocessing in python where I wish to have a
Pool of 4-5 processes running a method in parallel. The purpose of this is to
run a total of thousand Monte simulations (250-200 simulations per process)
instead of running 1000. I want each process to write to a common shared array
by acquiring a lock on it as soon as its done processing the result for one
simulation, writing the result and releasing the lock. So it should be a three
step process :

  1. Acquire lock
  2. Write result
  3. Release lock for other processes waiting to write to array.

Everytime I pass the array to the processes each process creates a copy of
that array which I donot want as I want a common array. Can anyone help me
with this by providing sample code?


Answer: Not tested, but something like that should work. The array and lock are shared
between processes.

    
    
    from multiprocessing import Process
    
    def f(array,lock):
        lock.acquire()
        #modify array here
        lock.release()
    
    if __name__ == '__main__':
        size=100
        multiprocessing.Array('i', size) #c type array
        lock=multiprocessing.Lock()
        p = Process(target=f, args=(arr,lock,))
        q = Process(target=f, args=(arr,lock,))
        p.start()
        q.start()
        q.join()
        p.join()
    

the documentation here
<https://docs.python.org/3.5/library/multiprocessing.html> has plenty of
examples to start with

