PHP Asynchronous call background calls are continuously executed disconnected/the browser title is very strange, because I do not know how to use a short sentence to summarize the following content. I just hope that PHP developers will find this introduction more easily through search engines. The core content of this time is centered around the ignore_user_abort function. PHP Asynchronous call background call continuous execution disconnect/browser
The title is very strange, because I do not know how to use a short sentence to summarize the following content. I just hope that PHP developers will find this introduction more easily through search engines.
The core content of this time is centered around the ignore_user_abort function.
Do? Web developers know that browsers and servers communicate over HTTP. This is a request and response model-based protocol. The browser sends a Request to a server through a URL. the Web server receives the Request, executes a program, and then responds to the Response, which is a string, this string conforms to the HTTP format and has two parts: HEAD and BODY.
There is a problem, Web? The server may execute a program in several milliseconds or minutes. If the program runs slowly and the user may not be patient, the browser will be closed. At this time, the server will receive a notification that the connection status changes, because HTTP? Is the top-level protocol, and there is a layer of TCP below, TCP? The connection is interrupted. If the connection is disconnected, the program on the server will immediately stop running.
Now the key to the problem is that the program on the server immediately stops execution. if the program only reads information, it is okay to say that it will not be read when it stops. What if it is a written program? For example, if a user submits a piece of text, the design of the server may be complicated and several tables need to be updated at the same time. However, for some reason, for example, one of the tables is locked by other processes, then this program will remain waiting. at this time, if the user closes the browser, the program will not wait and quit directly. The result is that the information is incomplete.
For example, the text submitted by the user must be written to the tables A, B, C, and D. writing to tables A and B may be completed in 0.1 milliseconds, however, if Table C is locked by another process, the current process will have to wait. when the user closes the browser and the current process exits, a situation will occur, table A and Table B have new content, but table C and Table D do not have this content, which leads to A serious data consistency problem!
Although database transactions can be used to roll back the status, the result is that the user's current commit is invalid.
I hope that as long as the user submits the file, it will be saved successfully. it may take too long to execute the file, but it does not matter if the user closes the browser.
So I found the ignore_user_abort function.
When ignore_user_abort (1) is called, the flag of ignoring the user exit is set, that is, the program should be executed to the end, unless exit is called in the program.
In fact, there is another function, register_shutdown_function, which can register a function or method and call it when the program Exits. it is similar to the onunload and onbeforeunload events in javascript.
I reference the relevant content of the Chinese PHP manual here:
Chapter 40. connection handling
Note: The following content applies to PHP 3.0.7 and later.
In PHP, the system maintains the connection status. There are three possible states:
0-NORMAL (NORMAL)
1-ABORTED (exit unexpectedly)
2-TIMEOUT (TIMEOUT)
When the PHP script runs normally, the connection is valid. When the remote client is disconnected, the ABORTED status mark will be opened. The interruption of remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP time limit (see the set_time_limit () function), the TIMEOUT status mark will be opened.
You can determine whether the script needs to exit when the client terminates the connection. Sometimes it is much more convenient to run the script completely, even if the script output is not accepted by a remote browser. By default, the script will exit when the remote client connection is interrupted. This process can be controlled by php. ini's ignore_user_abort or by the "php_value ignore_user_abort" and ignore_user_abort () functions in Apache. conf settings. If PHP is not told to ignore user interruptions, the script will be interrupted unless the trigger function is disabled through register_shutdown_function. When a remote user clicks the STOP button and the script tries to output data again, PHP will detect that the connection has been interrupted and call to disable the trigger function.
The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be changed by setting the "php_value max_execution_time" parameter in the max_execution_time or Apache. conf setting of php. ini or the set_time_limit () function. When the counter times out, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call the connection_status () function to check whether timeout has caused the function to be called. If timeout causes function call to be disabled, the function returns 2.
Note that the ABORTED and TIMEOUT statuses are valid at the same time. It is possible to tell PHP to ignore the user's exit operation. PHP will still note that the user has interrupted the connection but the script is still running. If the running time limit is reached, the script will be exited, and the set function to close and trigger will also be executed. The connection_status () function returns 3.
The PHP official website's online documentation contains the original English version. some user feedback and valuable information are provided below. here is the link:
? Http://www.php.net/manual/en/features.connection-handling.php