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

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

问题描述

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

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

这是什么意思?

这些软件包之间有什么区别?

What is the difference between these packages?

推荐答案

并发方法需要背景:

不同的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进行处理,然后接收结果并将其发送回用户.有多种方法可以将Web服务器与PHP链接在一起.对于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的环境而言,我要说的是,如果要在Apache Web服务器上使用PHP,则只需考虑这一点,在这种情况下,建议您使用Prefork MPM. Apache(不使用线程,因此PHP线程安全无关紧要),当您通过Apache + PHP的软件包系统安装Apache + PHP时,我所知道的所有GNU/Linux发行版都会为您做出决定.甚至提示您选择.如果要使用其他Web服务器,例如 nginx FastCGI 或在PHP完全不同于<网络服务器的em>外部,具有多个PHP进程,用于通过例如FastCGI.在这种情况下,线程安全也无关紧要.若要查看您的网站正在使用哪个版本,请在您的网站上放置一个包含<?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天全站免登陆