2

I want to share a large read only object between subprocess made by multiprocessing library on Linux. I don't want to use Manager() because that affects performance.

The large object is not an array and is actually a list of gmpy.xmpz from the gmpy2 library. This means using a shared array from the multiprocessing library doesn't work.

How to do that?

In the past, when I used python 2, I can use a global variable in the main process. The subprocess can read it without making copies of the object on linux, as long as I don't modify the object in any way.

But, somehow the trick doesn't work anymore after I recompile python 2 with intel compiler. I also migrate my code to python 3. The same trick still doesn't work.

old code:

import multiprocessing as mp
import numpy as np
def f(x):
    n = 0
    print(shared_obj[0], id(shared_obj))

def main():
    global shared_obj
    # 7Gb object. in the actual code, this is not an array
    shared_obj = np.zeros(7e9/4, dtype=np.int32)
    pool = mp.Pool(6)
    result = pool.map_async(f, range(10))
    pool.close()
    result.get()
    pool.join()

main()

Error:

self.pid = os.fork()
OSError: [Errno 12] Cannot allocate memory
hamster on wheels
  • 2,771
  • 17
  • 50
  • 2
    You're simply running out of memory due to the kernel conservatively reserving space for your processes. This answer is not strictly speaking a duplicate, but may address your issue: http://stackoverflow.com/a/15623171/1076479 (Not really sure how python2 versus python3 would have anything to do with it. I suspect your kernel was simply configured differently in the past.) – Gil Hamilton Feb 24 '17 at 17:43
  • Sorry that I forget to mention that I already used "echo 2 > /proc/sys/vm/overcommit_memory" and "echo 90 > /proc/sys/vm/overcommit_ratio" with root account. – hamster on wheels Feb 24 '17 at 17:47
  • Wait... somehow setting overcommit to` 1` instead of `2` seem to work for the test program. let me try the real program. – hamster on wheels Feb 24 '17 at 17:48
  • @GilHamilton It works. Thanks! – hamster on wheels Feb 24 '17 at 18:08

0 Answers0