我将尽早关闭与客户端的连接,代码如下:
static public function early_close( $output )
{
ignore_user_abort(true);
echo $output;
// Disable gzip compression in apache, as it can result in this request being buffered until it is complete,
// regardless of other settings.
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', 1);
}
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
header("Content-Encoding: none"); // To disable Apache compressing anything
// IF PHP-FM
// fastcgi_finish_request();
// flush all output
if( ob_get_level() > 0 )
{
ob_end_flush();
ob_get_level()? ob_flush():null;
flush();
}
// if you're using sessions, this prevents subsequent requests
// from hanging while the background process executes
if( session_id() )
{
session_write_close();
}
}
功能正常,但是在这个事件之后,如果某些脚本输出任何内容(无论是通过echo还是添加新的头),则脚本将从那一点停止执行。
我尝试在早期关闭后启动输出缓冲区,然后丢弃它,但它不起作用:
Server::early_close();
ob_start();
heavy_work();
ob_clean();
有什么想法吗?
使用php 5.3.x
die()
是被禁止的吗? - Anthonydie()
是没有帮助的,因为要求是一旦服务器接收到执行heavy_work
的指令就立即回复客户端。客户端不需要等待heavy_work
结束。如果将Server::early_close()
替换为die()
,则可以避免执行heavy_work()
。 - Pherrymasonheavy_work()
中所做的任何事情都会输出或修改头信息?请求的响应已经在flush()
时发送,因此您无法响应任何端点? - tomahaug