The previous article said: Web-server will create a thread (process) to process received PHP requests. (The difference between using threads or processes can be found in blog: http://blog.csdn.net/NRC_DouNingBo/archive/2011/06/29/6575807.aspx ). How can this request be processed? How can we let the web-server recycle this thread ??
The user requests data for a Web request. Therefore, some PHP scripts must eventually echo some data to return to the user requesting it (here the user refers to the browser ). There are also many articles on the Internet discussing the slow echo speed. In fact, this is not clear: what is the complete process of a PHP request.
For example, I write the following PHP script:
<? Php <br/> $ var ['key'] = "Temp"; <br/> echo json_encode ($ var); <br/>?>
Originally I thought: after the second echo statement is executed, the request is executed, and then the web-server recycles the thread. The question is: is the execution completed at this time? No!
① For ECHO, var_dump () and so on: Actually, the data should be returned to the user (browser. The echo execution process is as follows: it sends data to the user. The user receives the data and returns the feedback to inform him that the user has received the data. After the echo function receives this success signal, it will think that the function is executed after its mission is over.
This process may cause a lot of problems. For example, if the user's network is poor, the process of waiting for a successful signal to be sent will be slow, seriously affecting the execution speed of the PHP script, the dispute extended the execution time, And the execution thread of the request was unable to recycle. So for a specific project: Millions of requests request the same server at the same time. Because each execution time is a process, a large number of processing threads cannot be recycled in time, as a result, the server accumulates a large number of threads. This seriously affects Server Response !!
How can we solve these problems? To solve this problem, we must start from the root cause of the problem. The root cause is that the echo itself is slow and the PHP processing thread is not released.
① To handle slow echo, we can open the PHP output cache. That is, every time the echo returns the data to the user, we directly let PHP store it in the PHP output cache and give a successful response. At this time, the echo function will think that it has reached the user end and succeeded, the execution is complete.
This solution solves the slow echo processing problem. But after Echo is executed, the PHP processing thread is not released. Why? At this time, the PHP output cache still contains the data that has just been saved, but the PHP thread cannot be released. So he has to wait until the data in the output cache is sent out to release the thread. From this point of view: after using the method, the time for the thread to exist is not reduced at all, but the time consumed by the original ECHO is escaped to the output cache. At the same time, PHP cache has a size limit. If the echo data is larger than this limit, it cannot be stored in the output cache.
② The final solution is: we open the Apache output cache. When Echo is executed in this way: the data is directly sent to the Apache output cache and a success signal is returned. When the echo function receives a signal, it is considered to have been successfully executed. At this time, the PHP thread finds itself: the entire script has been executed, and no output cache is required to be sent. All of its work has been done, so it almost releases itself.
This method reduces the execution time of a PHP processing process and releases the thread as soon as possible, thus reducing the number of threads on the server !! The essence is: transfer the processing time from PHP to Apache! But the advantage is obvious: whether it is Apache or PHP threads to process this, it will consume the same time and other resources. However, using PHP requires additional thread consumption. After Apache output cache is Enabled: This greatly reduces the number of threads on the server !!
In fact, this method does not allow users to get data faster, that is, there is no optimization for the user itself, but it is a great optimization for the server, it accelerates the processing of each request and avoids problems caused by thread accumulation !!!