1

I have two objects

shared_array = numpy.zeros(20)

and

shared_matrix = scipy.sparse.csr_matrix((data, (row, col)),(20,20),dtype=numpy.float64)

which I want to be accessible from all processes created with

multiprocessing.Process(target = random_function, args = (shared_matrix, shared_array))

How is this done?

1 Answer 1

2

If you just want to access it, you can. You can read data from global variables across all processes created by multiprocessing.

However if you want to write into for example a dictionary (with caution not to overwrite) you can use shared memory objects. multiprocessing has a built in manager from where you can call all the shared memory objects such as lists, dicts.

You pass all your objects in arguments, so it will be available for all of the processes, however if you make changes inside the object it wont we permanent, as it would not be permanent with a simple function.

Simple example for a numpy array:

import numpy as np
import multiprocessing

a = np.zeros(5)

def func(b):
    ns.array += b
    return

manager = multiprocessing.Manager()
ns = manager.Namespace()
ns.array = np.zeros(5)
pool = multiprocessing.Pool(2)

list(pool.imap_unordered(func, [1,2]))
print(ns.array)

Will output [ 3. 3. 3. 3. 3.]

Here is another very detailed solution: https://stackoverflow.com/a/7908612/4555249

Sign up to request clarification or add additional context in comments.

5 Comments

I want to be able to read and write. I've seen the manager but do they work with numpy.array and scipy.sparse.csr_matrix ?
As i wrote you can read all global variables inside each process spawned by multiprocessing. You dont even have to pass them in as arguments.
I ll add a simple example in my answer
That example will output [3,3,3,3,3] sometimes. Because the array is not locked it can happen that there is a race condition and only one of the two function applications gets commited. The result can as well be [1,1,1,1,1] or [2,2,2,2,2], and maybe even anything in between (although I haven't seen it)
Manager should take care of the lock. I belive you will never see any other output then the 3333

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.