Recently, Xiaofeng encountered a problem in the development project, that is, blocking. The thinkphp framework is used and ajax is used to request an operation. Before ajax does not return results, other operations in the same program are invalid. Then I checked it online. If someone has the same problem as me, I will share the solution below.
When you discover several HTTP requests to the server at the same time, you may find that these requests may not be completed concurrently. The server queues these requests, resulting in PHP blocking. The most likely reason is that the script reads and writes session data. The session in PHP is stored by the file system by default. When the session file is read and written, the file storing the session is locked, at this time, other requests that need to read and write session data need to wait until the previous request is completed, resulting in PHP blocking. Fortunately, PHP provides session_write_close () function to end the current session and write data.
Simple session blocking demonstration
Create two php files: session_a.php and session_ B .php.
<? Php
// Session_a.php
Session_start ();
$ _ SESSION ['A'] = date ('H: I: s ');
// Session_write_close ();
Sleep (5 );
Echo $ _ SESSION ['A'];
<? Php
// Session_ B .php
Session_start ();
$ _ SESSION ['B'] = date ('H: I: s ');
// Session_write_close ();
Sleep (5 );
Echo $ _ SESSION ['B'];
When you access these two scripts at the same time, you will find that one of the scripts is 5 seconds behind the other. When we uncomment the session_write_close () function in the file, we can simultaneously access and find that two scripts can be executed at the same time.
Session lock processing mechanism
By the way, session_commit () is the alias of session_write_close (), that is, the former can be used to replace the latter.
When session_start () is called, the session processing mechanism opens or creates a seesion file by default, and locked the file immediately ). When session_commit () is called or the script is executed, the file is unlocked ).
The locking status has an important impact: the PHP script used for session requests is not executed in parallel, but separated. If a user initiates a request and initiates another request, the request is blocked until the previous request is complete.
Benefits of Session locking
Do not think that the so-called blocking phenomenon is a php bug. Of course not. On the contrary, sometimes separating execution is the correct method. Consider the shopping cart case:
The user initiates A request and the script reads the session data used to display the shopping cart items;
Before A completes the request, the user clicks the "add to shopping cart" button and sends a B request;
B waits for A to complete the request and adds data to the session;
What will happen if the session is not locked?
B did not wait for A to complete, read and write session data;
A requests are completed and written to the previously read session data, which overwrites the data written by B;
Therefore, we should consider the current actual environment when using session.
How does ThinkPHP solve session blocking?
Recently, I started to use the domestic PHP framework ThinkPHP, and I encountered a blocking problem because I did not carefully read the official documentation and the debugging failed for a long time. I almost gave up the framework in anger, it is found that there is a "SESSION_AUTO_START" configuration in the configuration item, which is used to automatically load the session, set it to FALSE decisively, and everything will return to normal.
When you need to use session, you can use the PHP built-in function:
Session_start ();
//...
Session_commit (); // or session_write_close ()
You can also use the TP style:
Session ('[start]');
//...
Session ('[pause]');