是否可以在每次调用时不加载引导机制?

发布于 2024-10-31 20:33:32 字数 276 浏览 2 评论 0原文

这不是 PHP 问题,但我的专业知识是 PHP 框架。


许多框架都有引导(类和文件的加载)机制。 (Drupal、Zend Framework 等)

每次发出请求时,都需要重复完整的引导加载过程。并且可以使用APC通过自动缓存一些中间代码来优化

。一般问题是:

对于任何语言,有没有办法不加载完整的引导过程?有没有什么方法可以在引导过程结束时“缓存”状态(或开始),以免再次加载所有内容? (也许答案是用其他语言/框架/模式)

在我看来效率极低。

This is not a PHP question, but my expertise is with PHP frameworks.


A lot of frameworks have a bootstrapping (loading of classes and files) mechanism. (Drupal, Zend Framework to name a few)

Everytime that you make a request, the complete bootloading process needs to be repeated. And it can be optimized using APC by automatically caching some intermediate code

The general question is:

For any language, is there any way to not load the complete bootstrapping process? Is there any way of "caching" the state (or starting at) at the end of the bootstraping process to not load everything again? (maybe the answer is in some other language/framework/pattern)

It looks to me as extremely inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-11-07 20:33:32

一般来说,每个进程很有可能执行一次引导/初始化代码,而不必为每个请求重新加载它。在您的具体情况下,我认为 PHP 不可能做到这一点(但我对 PHP 的了解有限)。我知道我已经认为这是对 PHP 架构的频繁批评......但公平地说,PHP 并不是唯一这样做的语言或框架。详细说明...

“为每个请求运行所有内容”的风格来自“CGI”脚本(参见

为了消除您发现的低效率问题,设计了第二种方法,它使用 Web 服务器本身的插件......对于 Apache,这包括用于 Perl 的 mod_perl 和用于 Python 的 mod_python (后者现在被用于 Python 的 mod_wsgi 取代)。使用这些插件,您可以配置服务器来识别每个进程加载一次的程序,然后执行必要的初始化,将其持久状态加载到内存中,并提供一个函数供服务器在有请求时调用。这可能会导致一些非常快的框架,以及诸如简单的数据库连接池之类的事情。

设计的另一个解决方案是用所需的语言编写一个Web服务器(通常是精简的),然后使用真正的Web服务器充当复杂请求的代理,同时仍然提供静态文件直接地。 Python 也经常使用此路径(通常通过“Paste”项目提供的服务器)。 Java 也通过 Tomcat Web 服务器使用它。这些服务器反过来提供与我在上一段中提到的大致相同的界面。

In general, it's quite possible to perform bootstrap / init code once per process, instead of having to reload it for every request. In your specific case, I don't think this is possible with PHP (but my knowledge of PHP is limited). I know I have seen this as a frequently criticism of PHP's architecture... but to be fair to PHP, it's not the only language or framework that does things this way. To go into some detail...

The style of "run everything for every request" came about with "CGI" scripts (c.f. Common Gateway Interface), which were essentially just programs that got executed as a separate process by the webserver whenever a request came in matching the file, and predefined environmental variables would be set providing meta information. The file could be basically any executable, written in any language. Since this was basically the first way anyone came up with of doing server-side scripting, a number of the first languages to integrate into a webserver used the cgi interface, Perl and PHP among them.

To eliminate the inefficiency you identified, the a second method was devised, which used plugins into the webserver itself... for Apache, this includes mod_perl for Perl, and mod_python for Python (the latter now replaced by mod_wsgi for Python). Using these plugins, you could configure the server to identify a program to load once per process, which then does the requisite initialization, loads it's persistent state into memory, and offers up a single function for the server to call whenever there is a request. This can lead to some extremely fast frameworks, as well as things such as easy database connection pooling.

The other solution that was devised was to write a web server (usually stripped down) in the language required, and then use the real webserver to act as a proxy for the complicated requests, while still serving static files directly. This route is also used frequently by Python (quite often via the server provided by the 'Paste' project). It's also used by Java, through the Tomcat webserver. These servers, in turn, offer approximately the same interface as I mentioned in the last paragraph.

生生漫 2024-11-07 20:33:32

简短的回答是:在 PHP 中没有好的方法可以跳过引导。 (从技术上讲,您可以运行一个 24/7 的 PHP 服务,该服务运行分叉的子进程来处理请求,但这不会让您的生活变得更好。)

一个好的框架不应该在引导方面做太多事情。在我个人使用的系统中,它只是为类注册一个自动加载函数,从 MemCache 加载配置设置,然后连接到数据库。

此时,它解析请求并将其发送到正确的控制器/操作。虽然每次创建新的路由器对象都是“浪费”,但无论引导过程是否在请求之间神奇地“缓存”,都需要完成处理请求的实际过程。

因此,我会测量启动页面和进入操作方法之间所需的时间,看看这是否是一个问题。如果框架正在执行与配置和类加载相关的昂贵操作,您应该能够通过将最终结果存储在内存缓存中来最大程度地减少这种情况。

请注意,您应该始终在生产中使用操作码缓存(例如 APC)和持久 SAPI(例如 php-fpm)。否则,启动和关闭会产生大量开销。

The short answer is: in PHP there's no good way to skip the bootstrapping. (Technically you could run a PHP service 24/7 that ran forked children to handle requests, but that's not going to make your life any better.)

A good framework shouldn't do much in bootstrapping. In my personal one that I use, it simply registers an autoload function for classes, loads the config settings from MemCache, and connects to a database.

At that point, it parses the request and sends it to the proper controller / action. While creating the new router object every time is a "waste," the actual process of handling the request needs to be done regardless if the bootstrapping process is magically "cached" between requests.

So I would measure the time it takes between starting the page and getting to the action method to see if it's even a problem. If the framework is doing expensive things related to configuration and class loading, you should be able to minimize that via storing the end results in memcache.

Note that you should always be using an opcode cache (e.g. APC) and a persistent SAPI (e.g., php-fpm) in production. Otherwise, there is a lot of overhead with starting up and shutting down.

記憶穿過時間隧道 2024-11-07 20:33:32

如果您想处理多个请求,我建议您研究 FastCGI 和 C/C++ 接口。通常它会带来很多问题(例如数据缓存/刷新、内存泄漏等),但可以提高性能10-100倍。

PHP更适合Web界面,如果您需要快速处理,那么您可以编写持久处理程序。

另请参阅 Java / Tomcat、Python 和 mod_perl。有些人还建议使用 xcache。

对于 PHP 框架,它们确实需要在核心中支持多请求结构,而且我不知道有任何框架这样做。

不过话虽如此,我很想有一个项目可以让 PHP 脚本响应循环内的多个请求。不是同时进行,而是绕过初始化。

您还可以查看 https://github.com/kvz/system_daemonhttp://gearman.org/

I would suggest you to look into FastCGI and C/C++ interface if you want to handle multiple requests. Usually it brings many problems (such as data caching / flushing, memory leaks etc), but can raise performance 10-100 times.

PHP is more suitable for web interface, and if you need fast-processing then you can write a persistent handler.

Also take a look at Java / Tomcat, Python and mod_perl. Some people have also suggested xcache.

For the PHP frameworks they do need to support a multi-request structure in the core, and I'm not aware of any framework doing that.

However said that, I'd love to have a project which would let PHP script to respond to multiple requests inside a loop. Not simultaneously, but bypassing the initialization.

Also you can take a look at https://github.com/kvz/system_daemon, and http://gearman.org/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文