PHP利用fastcgi_finish_request()函数实现异步操作,提高响应速度

WBOY
Release: 2016-06-20 12:27:57
Original
4451 people have browsed it

某些操作,如用户注册后邮件发送,记录日志等一些耗时操作可以转化为异步操作!当PHP运行在FastCGI模式是提供了fastcgi_finish_request()函数,看下面例子:

 
Copy after login
  1.  
  2. echo '输出给客户端的内容';
  3.  
  4. fastcgi_finish_request();
  5. sleep(3);
  6.  
  7. echo '放心吧,这里的内容并不会输出';
  8.  
  9. file_put_contents('log.txt', '这是客户端响应结束后,服务器段脚本继续执行后生成');


运行了次脚本,你会发现客户端输出上面一句话,fastcgi_finish_request()下面的内容并没有输出,但是却生成了文件,如此说明了调用了fastcgi_finish_request后,客户端响应就已经结束,但与此同时服务端脚本却继续运行。这在一定程度上提高了响应速度,当然更科学的做法是:使用fastcgi_finish_request()函数集成队列消息,可以把消息异步发 送到队列。
fastcgi_finish_reques()函数的缺点:
1.PHP FastCGI 进程数有限,正在处理异步操作的php-cgi进程,无法处理新请求;
2.如果并发访问量较大,php-cgi进程数用满,新访问请求,将没有php-cgi去处理。Nginx服务器会出现: 502 Bad Gateway。

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!