_reset_item_recursively(module, module_tree, new_module)ĭef _get_tree_references_to_reset_recursively(item, module_name, grayed_out_item_ids = None):Īttr_names = set(dir(item)) - _readonly_attrs Module_tree = _get_tree_references_to_reset_recursively(module, module._name_) # First, log all the references before reloading (because some references may be changed by the reload operation). # For the case when the module is actually a package :param inner_modules_also: Whether to treat ths module as a package as well, and reload all the modules within it. :param module: The module to reload (module reference, not the name) Module, it goes to the old instance of the module, and sets all the (not read-only) attributes, functions and classes What it does, is that aside from reloading a This function is a stronger form of importlib's `reload` function. If you don't want to mess with the GC, the original answer might be enough for you.ĭef reset_module(module, inner_modules_also=True): Note that this will not work if the GC is turned off, or if reloading data that's not monitored by the GC. To actually set all the references as well, I had to go into the garbage collector, and rewrite the references there. The solution from before is good for just getting the reset information, but it will not change all the references (more than reload but less then required). It's probably best to restart the server. It's not terribly nice but as long as you have a fully self-contained set of dependencies that doesn't leave references outside its codebase, it's workable. You'd have to go through and delete each sys.modules entry you wanted to be reloaded on next import, and also delete entries whose values are None to deal with an implementation issue to do with caching failed relative imports. The only way to do it in this case is to hack sys.modules, which is kind of unsupported. You can't do this with reload() because it will re-import each module before its dependencies have been refreshed, allowing old references to creep into new modules. If you have circular dependencies, which is very common for example when you are dealing with reloading a package, you must unload all the modules in the group in one go. And then reload modules that depend on the reloaded modules, recursively. If you have one-way dependencies, you must also reload all modules that depend on the reloaded module to get rid of all the references to the old code. If anything else has a reference to the module (or any object belonging to the module), then you'll get subtle and curious errors caused by the old code hanging around longer than you expected, and things like isinstance not working across different versions of the same code. Reload(module), but only if it's completely stand-alone. Web servers like Django's development server use this so that you can see the effects of your code changes without restarting the server process itself.Īs you noted in your question, you'll have to reconstruct Foo objects if the Foo class resides in the foo module. When targeting 3 or later, either reference the appropriate module when calling reload or import it. In 3.4, imp was deprecated in favor of importlib, and reload was added to the latter. In Python 3, reload was moved to the imp module. You can reload a module when it has already been imported by using the reload builtin function (Python 3.4+ only): from importlib import reload
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |