问题描述
我编写了一个简单的中继脚本,它连接到网络摄像头并从套接字读取数据,并使用打印功能输出这些数据.数据是已设置边界的 MJPG 数据.我只是输出读取到的数据.
问题是 PHP 似乎正在缓冲这些数据.当我将相机设置为 1 FPS 时,Feed 会冻结 7-8 秒,然后快速显示 8 帧.如果我将分辨率设置为很大,相机会以每秒 1 帧的速度移动.我假设当时正在发生一些缓冲(因为大尺寸会很快填满缓冲区,而小尺寸不会),我不知道如何禁用这种缓冲.有人知道怎么做吗?
代码:
ignore_user_abort(false);$boundary = "myboundary";//设置此项,以便 PHP 在长流期间不会超时设置时间限制(0);$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);如果 (!$socketConn)出口();stream_set_timeout($socketConn, 10);fputs ($socketConn, "GET/mjpeg HTTP/1.0
");//设置头信息header("缓存控制:无缓存");header("缓存控制:私有");header("Pragma: no-cache");header("内容类型:multipart/x-mixed-replace;boundary=$boundary");@ini_set('implicit_flush', 1);for ($i = 0; $i < ob_get_level(); $i++)ob_end_flush();ob_implicit_flush(1);stream_set_blocking($f2, false);//向客户端发送数据而 (connection_status() == CONNECTION_NORMAL){$chunk = fread($socketConn, 128);打印 $chunk;}fclose($socketConn);
tl;dr version
做两件事:
禁用用户空间输出缓冲区,或者...
在全球范围内,通过任一...
- 在 php.ini 中关闭
output_buffering
,或者 使用
关闭Apache配置中的output_buffering
php_flag "output_buffering" 关闭
- 在 php.ini 中关闭
或者只是你关心的脚本,通过...
- 调用
ob_end_flush()
,或 - 调用
ob_end_clean()
- 调用
另外,尽可能多地禁用服务器级输出缓冲区,方法如下:
- 在脚本开始时调用
ob_implicit_flush()
,或 - 在每个
echo
语句或其他将输出添加到响应正文的语句之后调用flush()
- 在脚本开始时调用
更长的版本
令人困惑的是,有两层缓冲可能是相关的,而 PHP 文档在区分这两者方面做得很差.
输出缓冲区
第一层通常被 PHP 文档称为输出缓冲区".这层缓冲只影响输出到 HTTP 响应的正文,而不影响标头.您可以使用 ob_start()
,然后使用 ob_end_flush 关闭它()
或 ob_end_clean()
.您还可以使用 output_buffering
php.ini 中的选项.
此选项的默认值用于 生产版本的 php.ini为4096,表示输出的前4096个字节会缓存在输出缓冲区中,此时将被刷新并关闭输出缓冲.
您可以通过在 php.ini 文件中将 output_buffering
设置为 Off
(或使用
php_flag "output_buffering" 关闭
在您的 Apache 配置中,如果您使用的是 Apache).或者,您可以通过在脚本开头调用 ob_end_clean()
或 ob_end_flush()
来为单个脚本禁用它.
写缓冲区和网络服务器缓冲区
除了输出缓冲区之外,还有 PHP 手册中称为写入缓冲区"的内容,以及您的 Web 服务器拥有的任何缓冲系统.如果您通过 mod_php
在 Apache 中使用 PHP,并且没有使用 mod_gzip
,则可以调用 flush()
来刷新这些;对于其他后端,它也可能有效,尽管手册对提供保证持谨慎态度:
说明
void 刷新 ( void )
刷新 PHP 的写入缓冲区以及 PHP 正在使用的任何后端(CGI、Web 服务器等).这会尝试将当前输出一直推送到浏览器,但有一些警告.
flush() 可能无法覆盖您的 Web 服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲都没有影响.它也不影响 PHP 的用户空间输出缓冲机制.这意味着您必须同时调用 ob_flush() 和flush() 用于刷新 ob 输出缓冲区(如果您正在使用它们).
还有几种方法可以让 PHP 在每次 echo
任何事情时自动调用 flush()
(或者做任何其他将输出回显到响应体的事情)).
首先是调用ob_implicit_flush()
.请注意,此函数的名称具有欺骗性;给定它的 ob_
前缀,任何有理智的人都会期望它会影响输出缓冲区",就像 ob_start
、ob_flush
等一样.但是,不是这种情况;ob_implicit_flush()
和 flush()
一样,影响服务器级别的输出缓冲区,并且不会与其他 ob_ 函数.
第二种是通过设置
implicit_flush
标记到你的 php.ini 中的 On
.这相当于在每个脚本开始时调用 ob_implicit_flush()
.请注意,手册建议不要这样做,隐晦地引用了严重的性能影响",其中一些我在此相关答案.
I wrote a simple relay script that connects to a web camera and reads from the socket, and outputs this data using the print function. The data is MJPG data with boundaries already setup. I just output the data that is read.
The problem is PHP seems to be buffering this data. When I set the camera to 1 FPS, the feed will freeze for 7-8 seconds, then quickly display 8 frames. If I set the resolution to a huge size, the camera move at more or less 1 frame per second. I assume then some buffering is happening (since huge sizes fill the buffer quickly, and low sizes don't), and I can't figure out how to disable this buffering. Does anyone know how to?
Code:
ignore_user_abort(false);
$boundary = "myboundary";
//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);
$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0
");
//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
stream_set_blocking($f2, false);
//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
$chunk = fread($socketConn, 128);
print $chunk;
}
fclose($socketConn);
解决方案
tl;dr version
Do two things:
Disable the userspace output buffer, either...
Globally, by either...
Turning off
output_buffering
in your php.ini, orTurning off
output_buffering
in your Apache config usingphp_flag "output_buffering" Off
or for just the script you care about, by either...
calling
ob_end_flush()
, orcalling
ob_end_clean()
Also, disable the server-level output buffer as much as you possibly can, by either:
calling
ob_implicit_flush()
at the start of your script, orcalling
flush()
after everyecho
statement or other statement that adds output to the response body
Longer version
Confusingly, there are two layers of buffering that may be relevant and the PHP documentation does a poor job of distinguishing between the two.
The output buffer
The first layer is usually referred to by the PHP docs as the 'output buffer'. This layer of buffering only affects output to the body of the HTTP response, not the headers. You can turn on output buffering with
ob_start()
, and turn it off with ob_end_flush()
or ob_end_clean()
. You can also have all your scripts automatically start with output buffering on using the output_buffering
option in php.ini.
The default value of this option for production versions of php.ini is 4096, which means that the first 4096 bytes of output will be buffered in the output buffer, at which point it will be flushed and output buffering is turned off.
You can disable this layer of buffering globally by setting
output_buffering
to Off
in your php.ini file (or using
php_flag "output_buffering" Off
in your Apache config, if you're using Apache). Alternatively, you can disable it for a single script by calling
ob_end_clean()
or ob_end_flush()
at the start of the script.
The write buffer, and the webserver buffer
Beyond the output buffer is what the PHP manual refers to as the 'write buffer', plus any buffering system your web server has. If you're using PHP with Apache through
mod_php
, and are not using mod_gzip
, you can call flush()
to flush these; with other backends, it might work too, although the manual is cagey about giving assurances:
There are also a couple of ways you can make PHP automatically call
flush()
every time you echo
anything (or do anything else that echoes output to the response body).
The first is to call
ob_implicit_flush()
. Note that this function is deceptively named; given its ob_
prefix, any reasonable person would expect that it would affect the 'output buffer', as do ob_start
, ob_flush
etc. However, this is not the case; ob_implicit_flush()
, like flush()
, affects the server-level output buffer and does not interact in any way with the output buffer controlled by the other ob_
functions.
The second is to globally enable implicit flushing by setting the
implicit_flush
flag to On
in your php.ini. This is equivalent to calling ob_implicit_flush()
at the start of every script. Note that the manual advises against this, cryptically citing "serious performance implications", some of which I explore in this tangentially related answer.
这篇关于如何在 PHP 中禁用输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!