Search code examples
pythonmodulepython-module

Python Modules - What happens during runtime?


I'm new to writing python modules and I'm curious what happens to your module during runtime? For example are modules loaded and unloaded when they're needed? The specific case I'm running into is this:

I've built some module that utilizes a in memory cache, the module is called from an exported function that looks at the cache, has some logic, then returns a Boolean based on the value in the cache. In this specific case the cache is updated every time the function gets called, would this cache persist across those multiple calls? Or is the module unloaded when it's not being used, therefore clearing my cache from memory and freshly loaded when the module is being accessed again?

To be a bit more broad, what is the behavior I can expect of my module throughout the run time of my python application?


Solution

  • When you import a module, its code is executed and the resulting module gets placed in sys.modules

    After that, the module is then assigned to a variable with the name of the module. For example, if you do import os, the module is inserted into sys.modules with the key os and is assigned to a global variable called os.

    In normal operation, the module is never unloaded, so you don't have to worry about data being lost or anything like that. That said, there are function like importlib.reload that can, in certain circumstances, cause the module to lose data.

    As long as you don't mess around with the internals, though, you can safely assume that the data in your cache stays valid throughout the entire runtime of your program.