这是一种更好的做法 - 全球进口或本地进口

时间:2022-10-18 02:45:58

I am developing an app in django and I had a doubt if importing a library at the global level has any impact over the memory or performance than importing at the local ( per-function) level. If it is imported per function or view, the modules that are needed alone are imported saving space right? Or are there any negatives in doing so?

我正在开发django中的应用程序,我怀疑在全局级别导入库对内存或性能的影响是否比在本地(每个功能)级别导入。如果按功能或视图导入,单独需要的模块是否导入节省空间?或者这样做是否有任何负面影响?

3 个解决方案

#1


12  

You surely must have noticed that almost all Python code does the imports at the top of the file. There's a reason for that: the overhead of importing is minimal, and the likelihood is that you will be importing the code at some point during the process lifetime anyway, so you may as well get it out of the way.

您肯定必须注意到,几乎所有Python代码都会在文件顶部执行导入。这是有原因的:导入的开销很小,并且可能是您在流程生命周期中的某个时刻导入代码,所以您也可以将其排除在外。

The only good reason to import at function level is to avoid circular dependencies.

在函数级别导入的唯一好理由是避免循环依赖。

Edit Your comments indicate you haven't understood how web apps generally work, at least in Python. They don't start a new process for each request and import the code from scratch. Rather, the server instantiates processes as required, and each one serves many requests until it is finally killed. So again it is likely that during that lifetime, all the imports will end up being needed.

编辑您的评论表明您还没有理解Web应用程序通常如何工作,至少在Python中是这样。它们不会为每个请求启动新进程并从头开始导入代码。相反,服务器根据需要实例化进程,并且每个进程都会处理许多请求,直到它最终被终止。因此,有可能在这一生中,所有进口最终都需要。

#2


0  

When the script is run, it will store the modules in memory, i'm sure you understand that.

当脚本运行时,它会将模块存储在内存中,我相信你明白这一点。

If you're importing on a local scope, the module will be imported each time the client calls the function. But if the module is imported at a global level, there will be no need for that!

如果要在本地范围内导入,则每次客户端调用该函数时都将导入该模块。但是如果模块是在全球范围内导入的,那就没有必要了!

So, in this case: global import wins.

因此,在这种情况下:全局导入获胜。

#3


0  

Import Statement Overhead

From Python Performance Tips:

从Python性能提示:

It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.

将它们放在函数中以限制其可见性和/或减少初始启动时间通常很有用。尽管Python的解释器已经过优化,不能多次导入同一个模块,但在某些情况下,重复执行import语句会严重影响性能。

#1


12  

You surely must have noticed that almost all Python code does the imports at the top of the file. There's a reason for that: the overhead of importing is minimal, and the likelihood is that you will be importing the code at some point during the process lifetime anyway, so you may as well get it out of the way.

您肯定必须注意到,几乎所有Python代码都会在文件顶部执行导入。这是有原因的:导入的开销很小,并且可能是您在流程生命周期中的某个时刻导入代码,所以您也可以将其排除在外。

The only good reason to import at function level is to avoid circular dependencies.

在函数级别导入的唯一好理由是避免循环依赖。

Edit Your comments indicate you haven't understood how web apps generally work, at least in Python. They don't start a new process for each request and import the code from scratch. Rather, the server instantiates processes as required, and each one serves many requests until it is finally killed. So again it is likely that during that lifetime, all the imports will end up being needed.

编辑您的评论表明您还没有理解Web应用程序通常如何工作,至少在Python中是这样。它们不会为每个请求启动新进程并从头开始导入代码。相反,服务器根据需要实例化进程,并且每个进程都会处理许多请求,直到它最终被终止。因此,有可能在这一生中,所有进口最终都需要。

#2


0  

When the script is run, it will store the modules in memory, i'm sure you understand that.

当脚本运行时,它会将模块存储在内存中,我相信你明白这一点。

If you're importing on a local scope, the module will be imported each time the client calls the function. But if the module is imported at a global level, there will be no need for that!

如果要在本地范围内导入,则每次客户端调用该函数时都将导入该模块。但是如果模块是在全球范围内导入的,那就没有必要了!

So, in this case: global import wins.

因此,在这种情况下:全局导入获胜。

#3


0  

Import Statement Overhead

From Python Performance Tips:

从Python性能提示:

It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.

将它们放在函数中以限制其可见性和/或减少初始启动时间通常很有用。尽管Python的解释器已经过优化,不能多次导入同一个模块,但在某些情况下,重复执行import语句会严重影响性能。