我看到了不同的 PHP 二进制文件,比如非线程或线程安全的?
I saw different binaries for PHP, like non-thread or thread safe?
这是什么意思?
这些包有什么区别?
不同的 Web 服务器采用不同的技术来并行处理传入的 HTTP 请求.一种非常流行的技术是使用线程——也就是说,Web 服务器将为每个传入请求创建/专用一个线程.Apache HTTP Web 服务器支持多种模型来处理请求,其中一种(称为工作 MPM)使用线程.但它支持另一种称为 prefork MPM 的并发模型,它使用进程——也就是说,Web 服务器将为每个请求创建/专用一个进程.
Different web servers implement different techniques for handling incoming HTTP requests in parallel. A pretty popular technique is using threads -- that is, the web server will create/dedicate a single thread for each incoming request. The Apache HTTP web server supports multiple models for handling requests, one of which (called the worker MPM) uses threads. But it supports another concurrency model called the prefork MPM which uses processes -- that is, the web server will create/dedicate a single process for each request.
还有其他完全不同的并发模型(使用异步套接字和 I/O),以及将两个甚至三个模型混合在一起的模型.为了回答这个问题,我们只关注上面的两个模型,并以Apache HTTP服务器为例.
There are also other completely different concurrency models (using Asynchronous sockets and I/O), as well as ones that mix two or even three models together. For the purpose of answering this question, we are only concerned with the two models above, and taking Apache HTTP server as an example.
PHP 本身不响应实际的 HTTP 请求——这是 Web 服务器的工作.所以我们配置Web服务器将请求转发给PHP进行处理,然后接收结果并发回给用户.有多种方法可以使用 PHP 链接 Web 服务器.对于 Apache HTTP Server,最流行的是mod_php".这个模块实际上是 PHP 本身,但被编译为 web 服务器的模块,所以它被直接加载到里面.
PHP itself does not respond to the actual HTTP requests -- this is the job of the web server. So we configure the web server to forward requests to PHP for processing, then receive the result and send it back to the user. There are multiple ways to chain the web server with PHP. For Apache HTTP Server, the most popular is "mod_php". This module is actually PHP itself, but compiled as a module for the web server, and so it gets loaded right inside it.
还有其他方法可以将 PHP 与 Apache 和其他 Web 服务器链接起来,但 mod_php 是最流行的一种,也可以用来回答您的问题.
There are other methods for chaining PHP with Apache and other web servers, but mod_php is the most popular one and will also serve for answering your question.
您之前可能不需要了解这些细节,因为托管公司和 GNU/Linux 发行版已经为我们准备好了一切.
You may not have needed to understand these details before, because hosting companies and GNU/Linux distros come with everything prepared for us.
由于使用 mod_php,PHP 会直接加载到 Apache 中,如果 Apache 要使用其 Worker MPM(即使用线程)处理并发,那么 PHP 必须能够在同一个多线程环境中运行——这意味着,PHP 必须是线程安全的,才能正确使用 Apache!
Since with mod_php, PHP gets loaded right into Apache, if Apache is going to handle concurrency using its Worker MPM (that is, using Threads) then PHP must be able to operate within this same multi-threaded environment -- meaning, PHP has to be thread-safe to be able to play ball correctly with Apache!
此时,您应该会想好吧,所以如果我使用的是多线程 Web 服务器并且我打算将 PHP 直接嵌入其中,那么我必须使用 PHP 的线程安全版本".这是正确的想法.然而,碰巧的是,PHP 的线程安全备受争议.这是一个如果你真的真的知道你在做什么"的地方.
At this point, you should be thinking "OK, so if I'm using a multi-threaded web server and I'm going to embed PHP right into it, then I must use the thread-safe version of PHP". And this would be correct thinking. However, as it happens, PHP's thread-safety is highly disputed. It's a use-if-you-really-really-know-what-you-are-doing ground.
如果您想知道,我个人的建议是不要在多线程环境中使用 PHP,如果您有选择的话!
In case you are wondering, my personal advice would be to not use PHP in a multi-threaded environment if you have the choice!
仅谈到基于 Unix 的环境,我想说的是,如果您打算将 PHP 与 Apache Web 服务器一起使用,您只需要考虑这一点,在这种情况下,建议您使用 prefork MPMApache(不使用线程,因此 PHP 线程安全无关紧要)和我所知道的所有 GNU/Linux 发行版在您通过他们的软件包系统安装 Apache + PHP 时都会为您做出决定,而无需甚至提示您做出选择.如果您打算使用其他网络服务器,例如 nginx 或 lighttpd,无论如何您都无法选择将 PHP 嵌入其中.您将看到使用 FastCGI 或在完全使用 PHP 的不同模型中工作的等效方法 在 Web 服务器之外,具有多个 PHP 进程,用于通过例如响应请求快速CGI.对于这种情况,线程安全也无关紧要.要查看您的网站使用的版本,请放置一个包含 <?php phpinfo(); 的文件?>
在您的网站上查找 Server API
条目.这可以说是 CGI/FastCGI
或 Apache 2.0 Handler
.
Speaking only of Unix-based environments, I'd say that fortunately, you only have to think of this if you are going to use PHP with Apache web server, in which case you are advised to go with the prefork MPM of Apache (which doesn't use threads, and therefore, PHP thread-safety doesn't matter) and all GNU/Linux distributions that I know of will take that decision for you when you are installing Apache + PHP through their package system, without even prompting you for a choice. If you are going to use other webservers such as nginx or lighttpd, you won't have the option to embed PHP into them anyway. You will be looking at using FastCGI or something equal which works in a different model where PHP is totally outside of the web server with multiple PHP processes used for answering requests through e.g. FastCGI. For such cases, thread-safety also doesn't matter. To see which version your website is using put a file containing <?php phpinfo(); ?>
on your site and look for the Server API
entry. This could say something like CGI/FastCGI
or Apache 2.0 Handler
.
如果你也看一下 PHP 的命令行版本——线程安全并不重要.
If you also look at the command-line version of PHP -- thread safety does not matter.
最后,如果线程安全无关紧要,那么您应该使用哪个版本——线程安全的还是非线程安全的?坦率地说,我没有科学的答案!但我猜想非线程安全版本更快和/或错误更少,否则他们只会提供线程安全版本而不会费心给我们选择!
Finally, if thread-safety doesn't matter so which version should you use -- the thread-safe or the non-thread-safe? Frankly, I don't have a scientific answer! But I'd guess that the non-thread-safe version is faster and/or less buggy, or otherwise they would have just offered the thread-safe version and not bothered to give us the choice!
这篇关于PHP中的线程安全或非线程安全是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!