I have already read read some posts about my problem (like this one), but none of them could solve the issue. In my Flask App I download 32-64 images in parallel using Python built in multiprocessing (on 16 physical cores) like this:
pool = Pool(int(torch.multiprocessing.cpu_count()/2), maxtasksperchild=maxchilds)
download_func = partial(download)
img_dicts = pool.map(download_func, url_dicts)
pool.close()
pool.join()
gc.collect()
def download(url_dict):
try:
image = io.imread(url_dict["img_url"])
except Exception:
image = None
gc.collect()
return {"post_id": url_dict["post_id"], "img": image}
The download works fine and increases the speed significantly, except memory usage. During the download, Python allocates ~ 100gb of RAM, although only ~ 2gb are actually used. Each process allocates more than 6 gb of RAM, just for downloading maximum 4 images with ~ 300-700kb each. maxchildspertask were set to 3 to avoid high memory usage, but did not bring significant improvements.
I am running on Windows, Python 3.7, 64 bit
Do you experienced the same or do you have ideas, how i can fix the memory issue?
Edit
I just realized that even I use a download function in an seperate file, which is just imported when in the method it's needed, the imports,... of a service get loaded for each child process, even I use a if __name__ in ('__main__', '__test__', '__routes__', '__shell__', '__runserver__'):
guard in my main file application.py. This loading of the whole app is causing the memory issue. How can I prevent, that my subprocesses load the whole app, even if I use a guard in my main file?