Maison > Article > développement back-end > [转]浏览器退出之后php还会继续执行么?,php继续执行_PHP教程
原文链接:http://www.cnblogs.com/yjf512/p/5362025.html
前提:这里说的是典型的lnmp结构,nginx+php-fpm的模式
如果我有个php程序执行地非常慢,甚至于在代码中sleep(),然后浏览器连接上服务的时候,会启动一个php-fpm进程,但是这个时候,如果浏览器关闭了,那么请问,这个时候服务端的这个php-fpm进程是否还会继续运行呢?
今天就是要解决这个问题。
最简单的方法就是做实验,我们写一个程序:在sleep之前和之后都用file_put_contents来写入日志:
<code class="hljs cs"><?<span class="hljs-function">php <span class="hljs-title">file_put_contents(<span class="hljs-params"><span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX); sleep(<span class="hljs-number">3); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></code>
实际操作的结果是,我们在服务器sleep的过程中,关闭客户端浏览器,2222是会被写入日志中。
那么就意味着浏览器关闭以后,服务端的php还是会继续运行的?
老王和diogin提醒,这个可能是和php的ignore_user_abort函数相关。
于是我就把代码稍微改成这样的:
<code class="hljs cs"><?<span class="hljs-function">php <span class="hljs-title">ignore_user_abort(<span class="hljs-params"><span class="hljs-keyword">false); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX); sleep(<span class="hljs-number">3); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></span></code>
发现并没有任何软用,不管设置ignore_user_abort为何值,都是会继续执行的。
但是这里有一个疑问: user_abort是什么?
文档对cli模式的abort说的很清楚,当php脚本执行的时候,用户终止了这个脚本的时候,就会触发abort了。然后脚本根据ignore_user_abort来判断是否要继续执行。
但是官方文档对cgi模式的abort并没有描述清楚。感觉即使客户端断开连接了,在cgi模式的php是不会收到abort的。
难道ignore_user_abort在cgi模式下是没有任何作用的?
首先想到的是不是心跳问题呢?我们断开浏览器客户端,等于在客户端没有close而断开了连接,服务端是需要等待tcp的keepalive到达时长之后才会检测出来的。
好,需要先排除浏览器设置的keepalive问题。
抛弃浏览器,简单写一个client程序:程序连接上http服务之后,发送一个header头,sleep1秒就主动close连接,而这个程序并没有带http的keepalive头。
程序如下:
<code class="hljs swift">package main <span class="hljs-keyword">import "net" <span class="hljs-keyword">import "fmt" <span class="hljs-keyword">import "time" <span class="hljs-function"><span class="hljs-keyword">func <span class="hljs-title">main<span class="hljs-params">() { conn, <span class="hljs-number">_ := net.<span class="hljs-type">Dial(<span class="hljs-string">"tcp", <span class="hljs-string">"192.168.33.10:10011") fmt.<span class="hljs-type">Fprintf(conn, <span class="hljs-string">"GET /index.php HTTP/1.0\r\n\r\n") time.<span class="hljs-type">Sleep(<span class="hljs-number">1 * time.<span class="hljs-type">Second) conn.<span class="hljs-type">Close() <span class="hljs-keyword">return }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
服务端程序:
<code class="hljs cs"><?<span class="hljs-function">php <span class="hljs-title">ignore_user_abort(<span class="hljs-params"><span class="hljs-keyword">false); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX); sleep(<span class="hljs-number">3); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></span></code>
发现仍然还是一样,php还是不管是否设置ignore_user_abort,会继续执行完成整个脚本。看来ignore_user_abort还是没有生效。
那该怎么触发ignore_user_abort呢?服务端这边怎么知晓这个socket不能使用了呢?老王和diogin说是不是需要服务端主动和socket进行交互,才会判断出这个socket是否可以使用?
另外,我们还发现,php提供了connection_status和connection_aborted两个方法,这两个方法都能检测出当前的连接状态。于是我们的打日志的那行代码就可以改成:
<code class="hljs lisp">file_put_contents('/tmp/test.log', '<span class="hljs-number">1 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted() . PHP_EOL, FILE_APPEND | LOCK_EX)<span class="hljs-comment">;</span></span></span></code>
根据手册连接处理显示我们可以打印出当前连接的状态了。
下面还缺少一个和socket交互的程序,我们使用echo,后面也顺带记得带上flush,排除了flush的影响。
程序就改成:
<code class="hljs django"><span class="xml"><span class="php"><span class="hljs-meta"><?php ignore_user_abort(<span class="hljs-keyword">true); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'1 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted() . PHP_EOL, FILE_APPEND | LOCK_EX); sleep(<span class="hljs-number">3); <span class="hljs-keyword">for($i = <span class="hljs-number">0; $i < <span class="hljs-number">10; $i++) { <span class="hljs-keyword">echo <span class="hljs-string">"22222"; flush(); sleep(<span class="hljs-number">1); file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted(). PHP_EOL, FILE_APPEND | LOCK_EX); }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
很好,执行我们前面写的client。观察日志:
<code class="hljs basic"><span class="hljs-symbol">1 connection status: <span class="hljs-number">0abort:<span class="hljs-number">0 <span class="hljs-symbol">2 connection status: <span class="hljs-number">0abort:<span class="hljs-number">0 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1 <span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
终于制造出了abort。日志也显示后面几次的abort状态都是1。
但是这里有个奇怪的地方,为什么第一个2 connection status的状态还是0呢(NORMAL)。
我们使用wireshark抓包看整个客户端和服务端交互的过程
这整个过程只有发送14个包,我们看下服务端第一次发送22222的时候,客户端返回的是RST。后面就没有进行后续的包请求了。
于是理解了,客户端和服务端大概的交互流程是:
当服务端在循环中第一次发送2222的时候,客户端由于已经断开连接了,返回的是一个RST,但是这个发送过程算是请求成功了。直到第二次服务端再 次想往这个socket中进行write操作的时候,这个socket就不进行网络传输了,直接返回说connection的状态已经为abort。所以 就出现了上面的情况,第一次222是status为0,第二次的时候才出现abort。
我们也可以使用strace php -S XXX来进行验证
整个过程strace的日志如下:
<code class="hljs erlang">。。。 <span class="hljs-function"><span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0 <span class="hljs-title">lstat<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873651, ...}) = 0 <span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5 <span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873651, ...}) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX) = 0 <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"1 connection status: 0abort:0\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0 <span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"HTTP/1.0 200 OK\r\nConnection: clo"..., <span class="hljs-number">89, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = 89 <span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"111111111", <span class="hljs-number">9, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = 9 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0 <span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">3, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290) = 0 <span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = 5 <span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5 <span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873681, ...}) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX) = 0 <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 0abort:0\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0 <span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290) = 0 <span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = -1 EPIPE <span class="hljs-params">(Broken pipe) --- SIGPIPE {<span class="hljs-title">si_signo=SIGPIPE, <span class="hljs-title">si_code=SI_USER, <span class="hljs-title">si_pid=2819, <span class="hljs-title">si_uid=0} --- <span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5 <span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873711, ...}) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX) = 0 <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0 <span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290) = 0 <span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5 <span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873741, ...}) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX) = 0 <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(5) 。。。</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
我们照中看status从0到1转变的地方。
<code class="hljs erlang">... <span class="hljs-function"><span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = 5 ... <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 0abort:0\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0 <span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0 <span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290) = 0 <span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = -1 EPIPE <span class="hljs-params">(Broken pipe) --- SIGPIPE {<span class="hljs-title">si_signo=SIGPIPE, <span class="hljs-title">si_code=SI_USER, <span class="hljs-title">si_pid=2819, <span class="hljs-title">si_uid=0} --- <span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5 <span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873711, ...}) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR) = 0 <span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX) = 0 <span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30 <span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5) = 0</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
第二次往socket中发送2222的时候显示了Broken pipe。这就是程序告诉我们,这个socket已经不能使用了,顺便php中的connection_status就会被设置为1了。后续的写操作也都不会再执行了。
正常情况下,如果客户端client异常推出了,服务端的程序还是会继续执行,直到与IO进行了两次交互操作。服务端发现客户端已经断开连接,这个 时候会触发一个user_abort,如果这个没有设置ignore_user_abort,那么这个php-fpm的程序才会被中断。
至此,问题结了。