以下不管那种方法都会在后台占用一个进程,大并发时不推荐
set_time_limit(0); //不设置超时时间
//设置set_time_limit后不起作用,修改下php-fpm.conf中的request_terminate_timeout,可以注销掉
第1种
fastcgi_finish_request(); // 冲刷(flush)所有响应的数据给客户端,关闭连接(需要运行在FastCGI模式,windows下无效)
ignore_user_abort(boolean); //在关闭连接后,继续运行php脚本
//如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。
第2种
使用register_shutdown_function(); //注册一个会在PHP中止时执行的函数
此方法实际是要等执行函数完成才算最终结束,如下会实际执行3秒
function test3(){
echo "test3-1:".time()."<br>";
sleep(3);
file_put_contents('D:\11.txt',time());//要写绝对地址,否则执行结束环境会被改变
echo "test3-2:".time()."<br>";
}
function test(){
echo "test-1:".time()."<br>";
register_shutdown_function(array($this, "test3"));
echo "test-2:".time()."<br>";
}
test();
第3种
$str = date("Y-m-d H:i:s");
//禁用浏览器缓存
header('Cache-Control: no-cache');
//适用于Nginx服务器环境
header('X-Accel-Buffering: no');
//告诉浏览器数据长度,浏览器接收到此长度数据后就不再接收数据
header("Content-Length:" . strlen($str));
//告诉浏览器关闭当前连接,即为短连接
header("Connection: close");
ob_end_clean(); //禁止PHP缓存数据
ob_implicit_flush(1); //打开/关闭绝对刷送,不需要再调用flush()
echo $str;
//其他代码
更多推荐
php返回结果,后端继续执行
发布评论