PHP is single-threaded, how to handle a lot of HTTP access?
Just started to know that PHP can do Web site, never contact PHP thread problem, today suddenly learned that PHP is single-threaded, but suddenly think, PHP do the site is definitely for many people access, if the traffic is large, it is not a queue problem?
Is that true? or Apache in the processing of PHP programs will use the server to implement multi-threading it?
------Solution--------------------
PHP does not have many threads, so it is not possible to open another thread in the code to perform other tasks, like Java, there are multiple mainline logic executing at the same time. But this does not have much to do with large traffic, because the request comes in Nginx or Apache processing, and then calls PHP processing, The invocation can be done concurrently, opening many processes to access the script, and the process is different from the thread. Each user will correspond to a PHP script process for the server, after execution (the page is opened), the process is destroyed and the memory is freed.
------Solution--------------------
All PHP interpreters that are started are independent of each other.
The PHP code that is naturally interpreted is also independent of each other.
The so-called Singleton, just for the current set of PHP code to run
------Solution--------------------
References:
Quote: References:
PHP does not have many threads, so it is not possible to open another thread in the code to perform other tasks, like Java, there are multiple mainline logic executing at the same time. But this does not have much to do with large traffic, because the request comes in Nginx or Apache processing, and then calls PHP processing, The invocation can be done concurrently, opening many processes to access the script, and the process is different from the thread. Each user will correspond to a PHP script process for the server, after execution (the page is opened), the process is destroyed and the memory is freed.
In the singleton mode
if ($instance = = null) {
$instance = new instance ();
}
Will this block be processed because the server calls multiple PHP scripts, and two calls are made at the same time, resulting in a new two instance?
Yes, the singleton mode in PHP will only be in the current run of the script, you also pointed out, is to detect whether $instance is empty, this is only limited to the run of the script will not new out of multiple objects