首页 技术 正文
技术 2022年11月9日
0 收藏 448 点赞 2,189 浏览 1845 个字

让PHP不再阻塞当PHP作为后端处理需要完成一些长时间处理,为了快速响应页面请求,不作结果返回判断的情况下,可以有如下措施:

一、若你使用的是FastCGI模式,使用fastcgi_finish_request()能马上结束会话,但PHP线程继续在跑。

帮助

01020304050607080910 echo "program start."; file_put_contents('log.txt','start-time:'.date('Y-m-d H:i:s'), FILE_APPEND);fastcgi_finish_request();sleep(1);echo 'debug...';file_put_contents('log.txt', 'start-proceed:'.date('Y-m-d H:i:s'), FILE_APPEND); sleep(10);file_put_contents('log.txt', 'end-time:'.date('Y-m-d H:i:s'), FILE_APPEND);

这个例子输出结果可看到输出program start.后会话就返回了,所以debug那个输出浏览器是接收不到的,而log.txt文件能完整接收到三个完成时间。

二、使用fsockopen、cUrl的非阻塞模式请求另外的网址

帮助

12345678 $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);if (!$fp) die('error fsockopen');stream_set_blocking($fp,0);$http = "GET /save.php  / HTTP/1.1\r\n";    $http .= "Host: www.example.com\r\n";    $http .= "Connection: Close\r\n\r\n";fwrite($fp,$http);fclose($fp);

利用cURL中的curl_multi_*函数发送异步请求

帮助

123456 $cmh = curl_multi_init();$ch1 = curl_init();curl_setopt($ch1, CURLOPT_URL, "http://localhost:6666/child.php");curl_multi_add_handle($cmh, $ch1);curl_multi_exec($cmh, $active);echo "End\n";

三、使用Gearman、Swoole扩展
Gearman是一个具有php扩展的分布式异步处理框架,能处理大批量异步任务;
Swoole最近很火,有很多异步方法,使用简单。(尘缘注:号称重新定义PHP,把NodeJS喷得体无完肤。Swoole工具虽好,却感觉是扩展本身跟NodeJS没可比性)

四、使用redis等缓存、队列,将数据写入缓存,使用后台计划任务实现数据异步处理。
这个方法在常见的大流量架构中应该很常见吧

五、极端的情况下,可以调用系统命令,可以将数据传给后台任务执行,个人感觉不是很高效。

帮助

12 $cmd = 'nohup php ./processd.php $someVar >/dev/null  &';`$cmd`

六、外国佬的大招,没看懂,php原生支持
http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html

七、安装pcntl扩展,使用pcntl_fork生成子进程异步执行任务,个人觉得是最方便的,但也容易出现zombie process。

帮助

0102030405060708091011121314151617 if (($pid = pcntl_fork()) == 0) {    child_func();    //子进程函数,主进程运行} else {    father_func();   //主进程函数} echo "Process " . getmypid() . " get to the end.\n"; function father_func() {    echo "Father pid is " . getmypid() . "\n";} function child_func() {    sleep(6);    echo "Child process exit pid is " . getmypid() . "\n";    exit(0);}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287