在CGI中使用模块之前加载模块有什么速度优势吗?(Are there any speed benefits when loading modules just before usage in CGI?

编程入门 行业动态 更新时间:2024-10-24 04:45:41
在CGI中使用模块之前加载模块有什么速度优势吗?(Are there any speed benefits when loading modules just before usage in CGI?)

我正在使用Python中的CGI构建Web应用程序。 由于脚本foo.py需要在每次调用mypage.com/foo.py时加载,我正在寻找一些方法来加速初始化。

使用Django或web.py等框架时,我总是在文件顶部加载模块。 假设我正在导入函数func并在我的请求处理程序中使用它:

from module import func # ... def request_handler(user_input): if user_input == 1: func()

在这种情况下,当有人加载某些页面而不是加载完整的python文件时,框架将只调用request_handler 。 在CGI中,每次访问URL时都会加载脚本,这意味着每次都会从module导入func 。 由于func不会被始终调用,因为user_input可能与1不同,我选择在必要时导入它,如下所示:

if user_input == 1: from module import func func()

我是在提高绩效还是一样?

I'm building a web application using CGI in Python. Since the script foo.py needs to be loaded every time it's called at mypage.com/foo.py I'm looking for some ways to speed up the initialization.

When using a framework such as Django or web.py I always load the modules at the top of the file. Let's say I'm importing the function func and using it in my request handler:

from module import func # ... def request_handler(user_input): if user_input == 1: func()

In this case the framework will just call the request_handler when someone loads some page instead of loading the full python file. In CGI the script is loaded every time the URL is visited, that means func will be imported from module every time too. Since func won't be called always, because user_input may be a different value than 1 I opted to import it just when necessary, like this:

if user_input == 1: from module import func func()

Am I making a performance improvement or it's just the same?

最满意答案

是的,如果不以其他方式导入module ,那么您将提高边际速度。

Yes, you are making a marginal speed improvement, provided that module would not be imported otherwise.

更多推荐

本文发布于:2023-07-24 20:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   有什么   加载   速度   优势

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!