php 伪异步 fastcgi_finish_request

之前我们项目都是用的spawn-cgi的形式管理php的fastcgi,但是发现效率不是很高,这段时间我有空闲就研究下fpm

 

按照我的习惯,我喜欢看官方文档,当我看到这个的时候眼前一亮 

http://php-fpm.org/wiki/Features#request_slowlog_timeout

这玩意是个好东东啊,

This feature allows you to speed up implementation of some php queries. Acceleration is possible when there are actions in the process of script execution that do not affect server response. For example, saving the session in memcached can occur after the page has been formed and passed to a web server. fastcgi_finish_request() is a php feature, that stops the response output. Web server immediately starts to transfer response "slowly and sadly" to the client, and php at the same time can do a lot of useful things in the context of a query, such as saving the session, converting the downloaded video, handling all kinds of statistics, etc.

 

简单来说就是当你的后续工作不影响程序输出的话,可以在程序输出后里面调用 fastcgi_finish_request函数返回结果给client并释放掉client的http链接,但是同时php脚本并没有停止,还是会继续执行后续的工作

 

写个简单的demo

 

浏览器访问可以看到只输出了 “output before func

而我们 vim /tmp/test.txt的时候赫然发现了 hello,world的字样

但是当我们用命令行调用该文件的时候会发生什么呢?看图

这时候发现fastcgi_finish_request并没有起作用。

 

这个函数当一个伪异步还是蛮不错的,但是只能在php-fpm的环境下运行。非php-fpm要注意

posted on 2013-06-08 11:45  xiaorao  阅读(519)  评论(0编辑  收藏  举报

导航