0

I've been going through PHP's source code and the mysql_pconnect function and noticed it's using some kind of HashTable persistent_list which is defined in zend_globals.

The question is, how are this globals and variables are preserved across requests when PHP is set as mod apache/fcgi. If it's a new PHP process spawned for every request those variables should not be preserved.

4

2 回答 2

1

PHP-FPM 重用上下文,允许持久连接在 FCGI 中工作。值得考虑的是 PHP-FPM 最初会创建多个进程,因此您最终仍会获得多个连接,每个进程或上下文一个连接。

需要考虑的是,“持久连接弊大于利”是#php.pecl 中经常提到的一句话。连接的开销很轻,PHP 适合这种处理。在设计为持续运行的语言中,连接的开销可能更大(通常是因为,简单的连接会影响比程序直接操作的对象更多的对象),因此持久连接或池连接是有意义的。PHP 的设计目的是尽可能快地完成所有事情,而且它使用了多少资源来做这件事——一切都必须在大约 250 毫秒内运行——连接到 mysql 不会像在其他框架中那样触发一堆钩子语言,所以开销非常小。此外,

于 2012-12-26T11:21:50.253 回答
-1

The question is, how are this globals and variables are preserved across requests when PHP is set as mod apache/fcgi.

As apache module, they are shared because apache httpd itself is still running. That running process had spawned PHP and will spawn other PHPs in the future. As the child threads can relate to their parent, it can and is stored in the parents memory space.

As FCGI, this could be technically possible as long as the binary is already running, however this has not been implemented with the FPM. I would also say, it makes no real sense for FCGI, too, because it's per script and also timeouts are involved, so the life-span is much shorter as with the whole webserver.

于 2012-12-24T16:09:24.657 回答