PHP 中什么是线程安全或非线程安全? [英] What is thread safe or non-thread safe in PHP?

查看:22
本文介绍了PHP 中什么是线程安全或非线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了不同的 PHP 二进制文件,例如非线程或线程安全的?

I saw different binaries for PHP, like non-thread or thread safe?

这是什么意思?

这些包有什么区别?

推荐答案

需要的并发方法背景:

不同的 Web 服务器采用不同的技术来并行处理传入的 HTTP 请求.一种非常流行的技术是使用线程——也就是说,Web 服务器将为每个传入的请求创建/专用一个线程.Apache HTTP Web 服务器支持多种处理请求的模型,其中之一(称为工作 MPM)使用线程.但它支持另一种称为 prefork MPM 的并发模型,该模型使用进程——也就是说,Web 服务器将为每个请求创建/专用一个进程.

Needed background on concurrency approaches:

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 和其他网络服务器链接起来,但 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 时为您做出决定,而无需甚至提示你做出选择.如果您打算使用其他网络服务器,例如 nginxlighttpd,无论如何您都无法选择将 PHP 嵌入其中.您将考虑使用 FastCGI 或在 PHP 完全外部具有多个 PHP 进程的 Web 服务器,用于通过例如响应请求快速CGI.对于这种情况,线程安全也无关紧要.要查看您的网站使用的是哪个版本,请放置一个包含 <?php phpinfo(); 的文件.?> 并查找 Server API 条目.这可以说类似于 CGI/FastCGIApache 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 中什么是线程安全或非线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆