我正在使用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.
更多推荐
发布评论