New_sitem = getattr(new_item, sitem_name) _reset_item_recursively(module, new_module, module._name_, reset_items)ĭef _reset_item_recursively(item, new_item, module_name, reset_items=None):Īttr_names = set(dir(item)) - _readonly_attrs If you don't want to mess with the GC, the original answer might be enough for you. 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. Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.Īs you noted in your question, you'll have to reconstruct Foo objects if the Foo class resides in the foo module.The names in the module namespace are updated to point to any new or changed objects.As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.The init function of extension modules is not called a second time. Python module’s code is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module’s dictionary by reusing the loader which originally loaded the module.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. 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. In Python 3, it was moved to the imp module. You can reload a module when it has already been imported by using importlib.reload(): from importlib import reload # Python 3.4+
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |