I have an Amazon Linux image that I use for some ML tasks, which use Python heavily. When I spin up a new machine, the first time I import all the python packages (numpy, scipy, scikit-learn, xarray, etc.) take a long time-- if I import absolutely everything, it's about 80 seconds. Subsequent imports, even in separate Python processes on the same VM, are extremely fast: 1-2 seconds for everything.
I'd like to eliminate that minute-plus on startup if I could; I only pay the performance cost once per VM, but the VMs are short-lived (typically for one job, perhaps 45-60 minutes).
My original assumption was that this was related to __pycache__; that the minute the first time was compiling and caching the bytecode of all the libraries, and the subsequent runs were faster because all that was already finished. So I added a script to my image builder, that imported every single module I ever reference as part of creating the image. However, the results were not all as I expected, and there was no performance gain:
- Running the script as part of the image builder is almost instant (I was not expecting this).
- Running the script does create the expected
__pycache__files alongside their sources in site-packages. - The
__pycache__files exist on systems created from the image, before any python has ever been run. - Running the script on a system newly created from the image still takes over a minute, even though the
__pycache__files are already in place. - Running the script on the same system a second time takes under a second.
<module>.__cached__on the system created from the image appears to refer to the correct .pyc file in the correct__pycache__folder that was in place from the image.- The dates of all the .pyc files in the
__pycache__folders reflect the time of image creation, not of VM instantiation.
So it seems that while my "import everything" script does result in creating __pycache__ files, it doesn't actually have any impact at all on the startup time on a system created from an image with those files. Is there something else that is persisted across python sessions on the same machine other than the .pyc files? Is there a different cache of .pyc files other than that stored in site-packages that might be being checked?