Simple talk about the reload Operation _php Skill in PHP

Source: Internet
Author: User
Tags fpm sleep

Objective

A lot of seniors have warned us, reload can guarantee the smoothness of the whole process, the so-called smoothness refers to in the process of reload, the old process in the processing of the current request will not terminate prematurely. For many years, I have never questioned this, until one day, when I reload, there were 502 mistakes, so I had to rethink.

How do you reproduce the problem? Let's write a simple script to simulate:

<?php Sleep

(one);
echo "foo";

? >

At this point in the browser to browse the Web site, and then immediately perform reload operation, you can see 502 errors.

Is PHP so weak? Even the basic smoothness of reload is not guaranteed? The answer is, of course, negative, and in fact process_control_timeout we can achieve our goals through parameters. Unfortunately, this parameter defaults to 0, which is not effective, this article set it to 10s. Re-execute the previous experimental steps, which normally output the results. But if you do a few more experiments, you may find that when we reload, the sleep immediately ended, because sleep received a signal from reload and returned directly, let's rewrite the script:

<?php Sleep

(one);
echo "foo";
Sleep (one);
echo "Bar";

? >

Re-perform the previous steps, and you'll find that 502 errors are coming up again. This is because reload the first sleep immediately ended, but the second one is still valid and exceeds process_control_timeout the time limit. If we process_control_timeout set it to 12s, then it's good.

So, we just have to process_control_timeout set a reasonable number can guarantee the smoothness of the reload operation, but in the end how much is a reasonable value? Too small words may not work, too big words will have side effects? Let's repeat the last experiment with a question, but this time we'll add a surveillance:

shell> watch-n1 ' ps aux | grep php[-]fpm '

The purpose of this monitoring is to observe the changes in the number of PHP-FPM processes during the reload process, in order to make the effect more obvious, it is recommended to change the PHP-FPM startup mode to static mode, while the number of processes is not too much.

When we repeat the last experiment, it turns out that in addition to the process that is executing the request, the other processes are killed directly, and the new process does not start immediately, so that the new process does not complete the startup process until the last old process finishes executing. In the meantime, if there are other requests coming in, then there is no doubt that it will not be immediately responsive.

According to our experiment, we can draw a conclusion: By default, PHP-FPM can not guarantee smooth execution of reload operation, must set a reasonable process_control_timeout only line, but also need to note that its value can not be set too large, otherwise the system may appear more serious request blocking problem.

Summarize

The above is about reload in PHP all the content of the operation, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.