python (django) 请求到底是如何发生的?它必须重新解析所有代码库吗?
使用像 python(或 php)这样的脚本语言,事情不会像 .net 或 java 那样编译成字节码。
那么这是否意味着对于每个请求,它都必须遍历整个应用程序并解析/编译它?或者至少是给定调用堆栈所需的所有代码?
With a scripting language like python (or php), things are not compiled down to bytecode like in .net or java.
So does this mean that on every request, it has to go through the entire application and parse/compile it? Or at least all the code required for the given call stack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误:在 Python 中
导入
的所有内容都会编译为字节码(如果您可以写入包含要导入的源代码的目录,则保存为.pyc
文件 - 标准库&c 通常是预编译的,当然取决于安装选择)。只需保持主脚本简短(导入一些模块并调用其中的函数),您将在整个过程中使用编译的字节码。 (Python 的编译器被设计得非常快——这意味着它不会做很多其他合理的优化——但完全避免它仍然更快;-)。Wrong: everything you
import
in Python gets compiled to bytecode (and saved as.pyc
files if you can write to the directory containing the source you're importing -- standard libraries &c are generally pre-compiled, depending on the installation choices of course). Just keep the main script short and simple (importing some module and calling a function in it) and you'll be using compiled bytecode throughout. (Python's compiler is designed to be extremely fast -- with implications including that it doesn't do a lot of otherwise reasonable optimizations -- but avoiding it altogether is still faster;-).当作为 CGI 运行时,是的,需要为每个请求加载整个项目。 FastCGI 和 mod_wsgi 将项目保存在内存中并通过套接字与之通信。
When running as CGI, yes, the entire project needs to be loaded for each request. FastCGI and mod_wsgi keep the project in memory and talk to it over a socket.