如何实现 Python 的惰性导入-lazy import

开发 前端
众所周知,Python 应用程序在执行用户的实际操作之前,会执行 import 操作,不同的模块可能来自不同的位置,某些模块的运行可能非常耗时,某些模块可能根本不会被用户调用,因此很多模块的导入纯粹是浪费时间。

如果你的 Python 程序程序有大量的 import,而且启动非常慢,那么你应该尝试懒导入,本文分享一种实现惰性导入的一种方法。虽然 PEP0690[1] 已经提案让 Python 编译器(-L) 或者标准库加入这个功能,但目前的 Python 版本还未实现。

众所周知,Python 应用程序在执行用户的实际操作之前,会执行 import 操作,不同的模块可能来自不同的位置,某些模块的运行可能非常耗时,某些模块可能根本不会被用户调用,因此很多模块的导入纯粹是浪费时间。

因此我们需要惰性导入,当应用惰性导入时,运行 import foo 仅仅会把名字 foo 添加到全局的全名空间(globals())中作为一个懒引用(lazy reference),编译器遇到任何访问 foo 的代码时才会执行真正的 import 操作。类似的,from foo import bar 会把 bar 添加到命名空间,当遇到调用 bar 的代码时,就把 foo 导入。

写代码实现

那怎么写代码实现呢?其实不必写代码实现,已经有项目实现了懒导入功能,那就是 TensorFlow,它的代码并没有任何三方库依赖,我把它放到这里,以后大家需要懒导入的时候直接把 LazyLoader[2] 类复制到自己的项目中去即可。

源代码如下:

# Code copied from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/util/lazy_loader.py
"""A LazyLoader class."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib
import types


class LazyLoader(types.ModuleType):
  """Lazily import a module, mainly to avoid pulling in large dependencies.

  `contrib`, and `ffmpeg` are examples of modules that are large and not always
  needed, and this allows them to only be loaded when they are used.
  """

  # The lint error here is incorrect.
  def __init__(self, local_name, parent_module_globals, name):  # pylint: disable=super-on-old-class
    self._local_name = local_name
    self._parent_module_globals = parent_module_globals

    super(LazyLoader, self).__init__(name)

  def _load(self):
    # Import the target module and insert it into the parent's namespace
    module = importlib.import_module(self.__name__)
    self._parent_module_globals[self._local_name] = module

    # Update this object's dict so that if someone keeps a reference to the
    #   LazyLoader, lookups are efficient (__getattr__ is only called on lookups
    #   that fail).
    self.__dict__.update(module.__dict__)

    return module

  def __getattr__(self, item):
    module = self._load()
    return getattr(module, item)

  def __dir__(self):
    module = self._load()
    return dir(module)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

代码说明:

类 LazyLoader 继承自 types.ModuleType,初始化函数确保惰性模块将像真正的模块一样正确添加到全局变量中,只要真正用到模块的时候,也就是执行 __getattr__ 或 __dir__ 时,才会真正的 import 实际模块,更新全局变量以指向实际模块,并且将其所有状态(__dict__)更新为实际模块的状态,以便对延迟加载的引用,加载模块不需要每次访问都经过加载过程。

代码使用:

正常情况下我们这样导入模块:

import tensorflow.contrib as contrib
  • 1.

其对应的惰性导入版本如下:

contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib')
  • 1.

PEP0690 建议的做法

PEP0690 的提案是在编译器( C 代码)层面实现,这样性能会更好。其使用方法有两种。

其一

一种方式是执行 Python 脚本时加入 -L 参数,比如有两个文件 spam.py 内容如下:

import time
time.sleep(10)
print("spam loaded")
  • 1.
  • 2.
  • 3.

egg.py 内容如下:

import spam
print("imports done")
  • 1.
  • 2.

正常导入情况下,会等 10 秒后先打印 "spam loaded",然后打印 "imports done",当执行 python -L eggs.py 时,spam 模块永远不会导入,应用 spam 模块压根就没有用到。如果 egg.py 内容如下:

import spam
print("imports done")
spam
  • 1.
  • 2.
  • 3.

当执行 python -L eggs.py 时会先打印 "imports done",10 秒之后打印 "spam loaded")。

其二

另一种方式是调用标准库 importlib 的方法:

import importlib 
importlib.set_lazy_imports(True)
  • 1.
  • 2.

如果某些模块不能懒加载,需要排除,可以这样

import importlib 
importlib.set_lazy_imports(True,excluding=["one.mod", "another"])
  • 1.
  • 2.

还可以这样:

from importlib import eager_imports

with eager_imports():
    import foo
    import bar
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

最后的话

经过专业人士在真实的 Python 命令行程序上做测试,应用惰性导入后,可以使启动时间提高 70%,内存使用减少 40%,非常可观了。

参考资料

[1]PEP0690: https://github.com/python/peps/blob/main/pep-0690.rst

[2]LazyLoader: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/util/lazy_loader.py

责任编辑:武晓燕 来源: Python七号
相关推荐

2021-08-12 15:45:23

Pythonimport模块

2025-01-16 08:17:36

2010-03-18 16:51:00

python语法入门

2017-05-02 11:30:44

JavaScript数组惰性求值库

2020-11-19 10:50:43

ImportPython代码

2022-02-17 14:34:10

viewport项目API

2023-12-13 10:12:40

Python函数lambda

2009-07-02 09:34:05

hibernate的l

2022-04-01 07:14:13

模块Pythonimport

2010-11-24 11:13:07

MySQL批量导入

2022-08-26 13:56:30

模块JavaScript

2017-04-24 20:30:47

数据库工具导入数据

2014-01-23 10:04:04

Python趣闻

2010-11-04 13:50:20

DB2在线导入

2010-06-09 10:09:39

MySQL 数据库导入

2017-01-12 15:42:53

HookPythonImport

2010-02-26 15:22:55

.NET Framew

2009-06-18 11:25:26

Hibernate L

2020-02-06 13:06:52

人工智能自行车啤酒

2009-07-02 09:38:17

Hibernate延时
点赞
收藏

51CTO技术栈公众号