I created an import hook (based on PEP 302), to dynamicly load python bytecode (compiled python code) from the database.
import dyn_imports.test #@UnresolvedImport
This works fine, but if I change the module and reload the module, the module reloads but I do not see the changes.
This is how I create the bytecode :
code_object = compile(db.Text(dyn.py_source).encode(), dyn.name.encode(), 'exec')
key_name = 'dyn_imports.' + name
dynmod = models.Dynmods(key_name = key_name, name = key_name, bytecode = db.Blob(marshal.dumps(code_object)))
dynmod.put()
reload(sys.modules[key_name]) # reload the updated code
The reload() executes the load_module part of the import hook.
This is the simplified version of the load_module function in the import hook class:
def load_module(self, mod_name):
mod = imp.new_module(mod_name)
mod.__loader__ = self
dynmod = models.Dynmods.get_by_key_name(mod_name) # load from DB
mod.__file__ = mod_name
exec marshal.loads(db.Blob(dynmod.bytecode)) in mod.__dict__
sys.modules[mod_name] = mod
return mod
When I shutdown the instance(s), it works fine.
Update : I also have tested the import loader using py source instead of bytecode. This did not work either. I have done most of the testing on the dev server.
I have created a Jinja2 module loader before, which loads Jinja compiled templates (= py source) from the datastore. When I re-create the Jinja environment and delete the modules (compiled templates) from sys.modules, the new modules are reloaded, without instance restart. So what am I doing wrong here?