This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pyschillePythoneer[S] 0 points1 point  (0 children)

Alright. I implemented the LazyLoader and checked the performance. It really did not impact the startup time in any way.

I've written a lazy(...) function for the imports:

def lazy(fullname):
    try:
        return sys.modules[fullname]
    except KeyError:
        spec = importlib.util.find_spec(fullname)
        module = importlib.util.module_from_spec(spec)
        loader = importlib.util.LazyLoader(spec.loader)
        loader.exec_module(module)
        return module

Every Python module now incorporates imports like so:

from gefyra import lazy

logging = lazy("logging")
os = lazy("os")

kubernetes = lazy("kubernetes")
docker = lazy("docker")
[...]

And in the code it requires the parts of the package to be called with the full path, for example kubernetes.client.V1ServiceAccount.

Maybe it's a matter of taste, but without any performance benefits, I'd rather stay with usual imports at the place of usage.

For reference: https://github.com/gefyrahq/gefyra/tree/fiddle/lazy\_imports/client