I am trying to create a child process in python 3.8.0 using multiprocessing module without inheriting the parent's memory. I am using spawn start method mp.set_start_method('spawn')
for this. But the memory usage of the child process is almost same as the parent process. Code snippets below
I am using code shared here for testing How can I restrict the scope of a multiprocessing process?
memtest.py
import multiprocessing as mp
import numpy as np
def foo(x):
import time
time.sleep(60)
if __name__ == "__main__":
mp.set_start_method('spawn')
dont_inherit = np.ones((500, 100))
for x in range(3):
mp.Process(target=foo, args=(x,)).start()
run using python3 memtest.py
memory usage from top
449m 28m 14m S 0.0 0.2 0:00.44 python3 memtest.py
34904 10m 5816 S 0.0 0.1 0:00.03 /srv/env/bin/python3 -c from multiprocessing.resource_tracker import main;main(5)
252m 26m 13m S 0.0 0.2 0:00.26 /srv/env/bin/python3 -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=6, pipe_handle=20) --multiprocessing-fork
252m 27m 13m S 0.0 0.2 0:00.21 /srv/env/bin/python3 -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=6, pipe_handle=22) --multiprocessing-fork
252m 26m 13m S 0.0 0.2 0:00.23 /srv/env/bin/python3 -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=6, pipe_handle=24) --multiprocessing-fork
I am using virtualenv with python3.8.0 on ubuntu18.04
$ python3 --version
Python 3.8.0
What is wrong in this approach of creating a child process? I need to create a lot of child processes that need to be light weight, I initially figured using mp's spawn approach would do this but it doesn't seem to be working.