how are concurrent requests handled in PHP (using - threads, thread pool or child processes)

前端 未结 3 1317
梦谈多话
梦谈多话 2021-02-11 17:32

I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer

How does server manages

相关标签:
3条回答
  • 2021-02-11 17:41

    As I know, every webserver has it's own kind of handling multpile simultanous request. Usually Apache2 schould fork a child process for each new request. But you can somehow configure this behaviour as mentioned in your linked StackOverflow answer.

    Nginx for example gets every request in one thread (processes new connections asyncronously like Node.js does) or sometimes uses caching (as configured; Nginx could also be used as a load balancer or HTTP proxy). It's a thing of choosing the right webserver for your application.

    Apache2 could be a very good webserver but you need more loadbalancing when you want to use it in production. But it also has good power when having multiply short lasting connections or even documents which don't change at all (or using caching).

    Nginx is very good if you expect many long lasting connections with somehow long processing time. You don't need that much loadbalancing then.

    I hope, I was able to help you out with this ;)

    Sources:

    https://httpd.apache.org/docs/2.4/mod/worker.html

    https://anturis.com/blog/nginx-vs-apache/

    I recommend you to also look at: What is thread safe or non-thread safe in PHP?

    0 讨论(0)
  • 2021-02-11 17:56

    After doing some research I ended up with below conclusions.

    It is important to consider how PHP servers are set to be able to get insights into it.For setting up the server and PHP on your own, there could be three possibilities:

    1) Using PHP as module (For many servers PHP has a direct module interface (also called SAPI))

    2) CGI

    3) FastCGI

    Considering Case#1 PHP as module, in this case the module is integrated with the web server itself and now it puts the ball entirely on web server how it handles requests in terms of forking process, using threads, thread pools, etc.

    For module, Apache mod_php appears to be very commonly used, and the Apache itself handles the requests using processes and threads in two models as mentioned in this answer

    Prefork MPM uses multiple child processes with one thread each and each process handles one connection at a time.

    Worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time.

    Obviously, other servers may take other approaches but, I am not aware of same.

    For #2 and #3, web server and PHP part are handled in different processes, and how a web server handles the request and how it is further processed by application(PHP part) varies. For e.g.: NGINX may handle the request using asynchronous non-blocking I/O and Apache may handle requests using threads, but, how the request would be processed by FastCGI or CGI application is a different aspect as described below. Both the aspects i.e. how web server handles requests and how PHP part is processed would be important for PHP servers performance.

    Considering #2, CGI protocol has makes web server and application (PHP) independent of each other and CGI Protocol requires application and web server to be handled using different process and the protocol does not promote reuse of the same process, which in turn means a new process is required to handle each request.

    Considering#3, FastCGI protocol overcomes the limitation of CGI by allowing process re-use. If you check IIS FastCGI link FastCGI addresses the performance issues that are inherent in CGI by providing a mechanism to reuse a single process over and over again for many requests.

    FastCGI maintains compatibility with non-thread-safe libraries by providing a pool of reusable processes and ensuring that each process handles only one request at a time.

    That said, in case of FastCGI it appears that the server maintains a process pool and it uses the process pool to handle incoming client requests and since, the process pool does not require thread safe check, it provides a good performance.

    0 讨论(0)
  • 2021-02-11 18:03

    I think the answer depends on how the web server and the cgi deploy.

    In my company, we use Nginx as the web server and php-fpm as cgi, so the concurrent request is handled as process by php-fpm, not thread.

    We configure the max number of process, and each request is handled by a single php process, if more requests(larger than the max number of process) come , they wait.

    So, I believe PHP itself can support all of them, but how to use it, that depends.

    0 讨论(0)
提交回复
热议问题