PHP Async Child with background call to continue to execute Disconnect/browser

Source: Internet
Author: User
Tags php online
PHP asynchronously invokes a background call to continuously execute a disconnected/browser

The title is strange, because I don't know how to summarize this part of the text with a short sentence. Just hope that the developers of PHP more easily through the search engine to search this introduction.

The core of this is the function of Ignore_user_abort.

Do? The WEB developer knows that the connection between the browser and the server is communicated through the HTTP protocol. This is a protocol based on the request and response model. The browser initiates a request to a server via a URL, the WEB server receives the request, executes a program, and then responds (Response), a string that conforms to the HTTP protocol format with HEAD and BODY parts.

This one has a problem with the Web? The server executes a program that may be completed in a few milliseconds or it may not be finished in a few minutes. If the program executes slowly, the user may not have the patience to wait until the browser is closed. At this point, the server will receive notification of changes in the connection status because of HTTP? is the top-level protocol, the following is a layer of TCP protocol, TCP? Will know the connection is interrupted. Once the connection is broken, the service-side program stops executing immediately.

Now talk about the crux of the problem, the service side of the program immediately stop execution, if the program is just read the information is OK to say, stopped to read the chant. What if it's a written program? For example, the user submits a piece of text, perhaps the service side of the design is more complex, need to update several tables at the same time, but for some reason, for example, one of the table is locked by another process, then the program will wait, this time if the user close the browser, the program will not wait, and exit directly. The result is that this information is not kept intact.

To give a concrete example, the user submits the text needs to write to a, B, C, D Four table, write a, b two tables can be completed in 0.1 milliseconds, but the C table is locked by another process, the current process has to wait until the user closes the browser, the current process exits, then, will cause a situation, a, B The table has a new content, and the C, D table does not have this content, resulting in a serious problem of data consistency!

Although, using the database's transactions, you can make the state rollback, but the result is that the user's commit is invalid.

I hope that, as long as the user commits, it will be saved successfully, may encounter unexpected situation caused the execution time is too long, but the user close the browser is OK.

So, I found the function of Ignore_user_abort.

When calling Ignore_user_abort (1), it is set to ignore the user exit this flag bit, that is, in any case, the program must be executed, unless exit is called in the program.

In fact, there is another function, register_shutdown_function, which can register a function or method that is called when the program exits, some similar to the onunload and onbeforeunload events in JavaScript.

I quote the Chinese version of the PHP manual for the relevant content:

Chapter 40. Connection processing
Note: The following applies to PHP 3.0.7 and later versions.

Inside PHP, the system maintains a connection state with three possible conditions:

0-normal (Normal)

1-aborted (Abnormal exit)

2-timeout (Timeout)

The connection is valid when the PHP script is running normally in normal state. When the remote client disconnects, the token for the aborted status is opened. The interruption of a remote client connection is usually caused by the user tapping the STOP button. When the connection time exceeds the time limit of PHP (see Set_time_limit () function), the TIMEOUT status token is opened.

You can decide whether the script needs to exit when the client disconnects. Sometimes it is convenient to have the script run completely, even if no remote browser accepts the output of the script. The default scenario is that the script exits when the remote client connection is interrupted. This process can be controlled by the ignore_user_abort of the php.ini or by the corresponding "Php_value ignore_user_abort" and the Ignore_user_abort () function in the Apache conf settings. If you do not tell PHP to ignore the user's interruption, the script will be interrupted unless the shutdown trigger function is set by Register_shutdown_function (). With this shutdown trigger function, when the remote user taps the STOP button and the script attempts to output the data again, PHP will detect that the connection has been interrupted and invoke the shutdown trigger function.

Scripts can also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be changed by setting the "Php_value max_execution_time" parameter or the Set_time_limit () function in the php.ini max_execution_time or Apache conf settings. When the counter times out, the script will exit similar to the above connection interruption, and the previously registered shutdown trigger function will be executed at this time. In the shutdown trigger function, you can check whether the timeout causes the shutdown trigger function to be called by calling the Connection_status () function. If the timeout causes a call to close the trigger function, the function returns 2.

One thing to note is that the aborted and TIMEOUT states can be valid at the same time. This is possible when telling PHP to ignore the user's exit operation. PHP will still be aware of the situation where the user has interrupted the connection but the script is still running. If the run time limit is reached, the script will be exited and the set off trigger function will be executed. At this point, the function connection_status () is found to return 3.

The official website of PHP online document has the original English description, there are some user feedback, also very valuable, here is the link:

? http://www.php.net/manual/en/features.connection-handling.php

Transferred from: HTTP://WWW.LEAKON.COM/ARCHIVES/22

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.