PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial

WBOY
Release: 2016-07-13 09:45:54
Original
1511 people have browsed it

PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background

In the past two days, I have developed a PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial program that calls SVN to synchronize update updates on multiple servers. In order to avoid being blocked during commit for a long time, I have to find a way to only request the trigger without waiting for the program update to complete and return the results. This is so time-consuming. It took too long, so I studied how to make PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnect. After working on it all afternoon, I found a lot of problems. Fortunately, I finally solved them. The main reason was that Nginx was too messy. .


Without further ado, here’s the code:


PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial


<code class="hljs" java="">/**
 * 主动断开与客户端浏览器的连接
 * 如果是 Nginx 服务器需要输出大于等于 fastcgi_buffer_size 缓存的数据才能即时输出 header 断开连接, 若还是不行可尝试关闭 gzip
 * 如: fastcgi_buffer_size 64k; 即: 需要 64*1024 字符(可多不可少),
 * 可使用 str_repeat(&#39; &#39;, 65536); 另外 str_repeat(&#39;          &#39;, 6554); 这种方式其实生成速度更慢
 * @param null|string $str 当前输出的内容, 若无需输出则设置为空
 */
public function connectionClose($str = null) {
    $str = ob_get_contents() . $str;
    // 若实际输出内容长度小于该值将可能导致主动断开失败
    header(&#39;Content-Length: &#39;. strlen($str));
    Header::connectionClose();
    ob_start();
    echo $str;
    ob_flush();
    flush();
}</code>
Copy after login

Additional explanation:

There is generally no problem with apache. I used xampp on Windows to debug and found no problems at first. It turned out that it was Nginx on the server. It was dead or alive and crashed for an afternoon. It was later revealed that it was Nginx's fastcgi_buffer. question.

After testing N times under various conditions, there should be no bugs. . .


In addition, let’s talk about the problem of ignore_user_abort() function

When the browser is closed, determine whether the program will continue to execute in the background. (In the example below, you do not have to set it to never timeout limit 0 when testing, just set it for one or two minutes, otherwise It may take a long time to restart the HTTP service)


PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial


To put it simply, if you want the program to continue executing after the user's browser is closed, then you must add the following code:

<code bash="" class="hljs">ignore_user_abort(true);</code>
Copy after login

But it is slightly different depending on the situation of your subsequent program (mainly the while loop):

Generally in the program you can monitor the connection status for control:

<code bash="" class="hljs">$isAborted = connection_aborted();
$status = connection_status();
if (0 !== $status || $isAborted) {
    break;
}</code>
Copy after login

But there is a prerequisite for these two functions to work properly, that is, your program must have output content and be larger than the output cache of the current WebServer, so that it will work.

If you simply output a space echo ' '; it may have to loop thousands of times before it is judged, so in order to detect the status more immediately, you must output enough content each time it loops to trigger the status detection. .

So we often encounter a problem here: when the browser is disconnected, even if ignore_user_abort(true); is not used, the program will continue to execute because there is no output, and the infinite loop will continue to run. If it is set It's okay if it times out, otherwise you're really dead.


Post the test code below (the picture is posted mainly to prevent theft hehe~)


<code bash="" class="hljs">set_time_limit(0);

ignore_user_abort(true);

while (1) {
    echo str_repeat(&#39; &#39;, 65536);
    $isAborted = connection_aborted();
    $status = connection_status();
    file_put_contents(&#39;test.txt&#39;, &#39;time: &#39;. time() .&#39;; abroted:&#39;. $isAborted .&#39;; status: &#39;. $status);
    if (0 !== $status || $isAborted) {
        break;
    }
    sleep(2);
}</code>
Copy after login

You can try commenting out this sentence
// echo str_repeat(’ ‘, 65536);
Also
set_time_limit(0); It is best not to use 0

www.bkjia.comtruehttp: //www.bkjia.com/PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorialjc/1036923.htmlTechArticlePHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background in the past two days to make PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial call SVN to synchronize update Multiple server update procedures, in order to avoid com...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!