Is it possible to reuse python module already loaded in memory ?
Let us say I have scripts loader.py
and consume.py
. I am try to do next thing - invoke loader.py
and reuse it in consume.py
. First script should load in memory big file, second one will be invoked many times and use big file.
Can I achieve this? I am not familar with python but I guess, there should be a way to access loaded module (script) in memory.
My current implementation attempt looks like this:
loader.py
x = 3
print 'module loaded'
consume.py
from loader import x
print x
Update
I have tried to use importlib
as it was described here and here, but my loader module loads every time again. Below is my code for cosume.py
import importlib
module = importlib.import_module('loader')
globals().update(
{n: getattr(module, n) for n in module.__all__} if hasattr(module, '__all__')
else
{k: v for (k, v) in module.__dict__.items() if not k.startswith('_')
})
print(x)
Final goal
Invoke consume script many times from nodejs and not to load big file every time. Need to share data between script executions