PHP和Apache-flush&;ob_冲洗行为

PHP和Apache-flush&;ob_冲洗行为,php,apache,ubuntu,buffer,Php,Apache,Ubuntu,Buffer,我正在使用Apache2、PHP5和Firefox浏览器在我的Ubuntu笔记本电脑上测试一些ob_flush的行为 以下代码在第二次暂停时打印0-9之间的数字 每次它回显一个数字-它应该将其刷新回浏览器 ini_set('output_buffering', 'off'); // Turn off PHP output compression ini_set('zlib.output_compression', false); header('Content-type: text/html;

我正在使用Apache2、PHP5和Firefox浏览器在我的Ubuntu笔记本电脑上测试一些
ob_flush
的行为

以下代码在第二次暂停时打印0-9之间的数字

每次它回显一个数字-它应该将其刷新回浏览器

ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

header('Content-type: text/html; charset=utf-8');

echo memory_get_usage() / 1024 / 1024 . "<br>";

echo "The time is " . date("h:i:sa") . "<br>";
echo memory_get_usage() / 1024 / 1024 . "<br>";
for ($i = 0; $i < 10; $i++) {
    echo $i . " - " . memory_get_usage() / 1024 / 1024 . '<br />';
    flush();
    ob_flush();
    sleep(1);
}
sleep(1);
echo 'End ...<br />';
echo "The time is " . date("h:i:sa");
第二个浏览器窗口:

0.26750183105469
The time is 04:17:09pm
0.26776123046875
0 - 0.26792144775391
1 - 0.26787567138672
2 - 0.26787567138672
3 - 0.26787567138672
4 - 0.26787567138672
5 - 0.26787567138672
6 - 0.26787567138672
7 - 0.26787567138672
8 - 0.26787567138672
9 - 0.26787567138672
End ...
The time is 04:17:20pm
0.26509094238281
The time is 04:17:11pm
0.26535034179688
0 - 0.26551055908203
1 - 0.26546478271484
2 - 0.26546478271484
3 - 0.26546478271484
4 - 0.26546478271484
5 - 0.26546478271484
6 - 0.26546478271484
7 - 0.26546478271484
8 - 0.26546478271484
9 - 0.26546478271484
End ...
The time is 04:17:22pm
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

header('Content-type: text/html; charset=utf-8');

echo memory_get_usage() / 1024 / 1024 . "<br>";
$task = new TestTask(null);
$task->start();
0.26746368408203
The time is 04:20:34pm
2.3088989257812
0 - 2.3089752197266
1 - 2.3089294433594
2 - 2.3089294433594
3 - 2.3089294433594
4 - 2.3089294433594
5 - 2.3089294433594
6 - 2.3089294433594
7 - 2.3089294433594
8 - 2.3089294433594
9 - 2.3089294433594
End ...
The time is 04:20:45pm
0.26506042480469
The time is 04:20:45pm
2.3051910400391
0 - 2.3052673339844
1 - 2.3052215576172
2 - 2.3052215576172
3 - 2.3052215576172
4 - 2.3052215576172
5 - 2.3052215576172
6 - 2.3052215576172
7 - 2.3052215576172
8 - 2.3052215576172
9 - 2.3052215576172
End ...
The time is 04:20:56pm
您可以非常清楚地看到,这两个请求是由Apache同时处理的,内存使用量为0.26MB

一切都很好,大家都很开心:)

我将这段代码复制粘贴到一个类的方法中,突然Apache不再同时处理这些请求——而是一个接一个地处理

这是新代码:

0.26750183105469
The time is 04:17:09pm
0.26776123046875
0 - 0.26792144775391
1 - 0.26787567138672
2 - 0.26787567138672
3 - 0.26787567138672
4 - 0.26787567138672
5 - 0.26787567138672
6 - 0.26787567138672
7 - 0.26787567138672
8 - 0.26787567138672
9 - 0.26787567138672
End ...
The time is 04:17:20pm
0.26509094238281
The time is 04:17:11pm
0.26535034179688
0 - 0.26551055908203
1 - 0.26546478271484
2 - 0.26546478271484
3 - 0.26546478271484
4 - 0.26546478271484
5 - 0.26546478271484
6 - 0.26546478271484
7 - 0.26546478271484
8 - 0.26546478271484
9 - 0.26546478271484
End ...
The time is 04:17:22pm
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

header('Content-type: text/html; charset=utf-8');

echo memory_get_usage() / 1024 / 1024 . "<br>";
$task = new TestTask(null);
$task->start();
0.26746368408203
The time is 04:20:34pm
2.3088989257812
0 - 2.3089752197266
1 - 2.3089294433594
2 - 2.3089294433594
3 - 2.3089294433594
4 - 2.3089294433594
5 - 2.3089294433594
6 - 2.3089294433594
7 - 2.3089294433594
8 - 2.3089294433594
9 - 2.3089294433594
End ...
The time is 04:20:45pm
0.26506042480469
The time is 04:20:45pm
2.3051910400391
0 - 2.3052673339844
1 - 2.3052215576172
2 - 2.3052215576172
3 - 2.3052215576172
4 - 2.3052215576172
5 - 2.3052215576172
6 - 2.3052215576172
7 - 2.3052215576172
8 - 2.3052215576172
9 - 2.3052215576172
End ...
The time is 04:20:56pm
第二个浏览器窗口:

0.26750183105469
The time is 04:17:09pm
0.26776123046875
0 - 0.26792144775391
1 - 0.26787567138672
2 - 0.26787567138672
3 - 0.26787567138672
4 - 0.26787567138672
5 - 0.26787567138672
6 - 0.26787567138672
7 - 0.26787567138672
8 - 0.26787567138672
9 - 0.26787567138672
End ...
The time is 04:17:20pm
0.26509094238281
The time is 04:17:11pm
0.26535034179688
0 - 0.26551055908203
1 - 0.26546478271484
2 - 0.26546478271484
3 - 0.26546478271484
4 - 0.26546478271484
5 - 0.26546478271484
6 - 0.26546478271484
7 - 0.26546478271484
8 - 0.26546478271484
9 - 0.26546478271484
End ...
The time is 04:17:22pm
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

header('Content-type: text/html; charset=utf-8');

echo memory_get_usage() / 1024 / 1024 . "<br>";
$task = new TestTask(null);
$task->start();
0.26746368408203
The time is 04:20:34pm
2.3088989257812
0 - 2.3089752197266
1 - 2.3089294433594
2 - 2.3089294433594
3 - 2.3089294433594
4 - 2.3089294433594
5 - 2.3089294433594
6 - 2.3089294433594
7 - 2.3089294433594
8 - 2.3089294433594
9 - 2.3089294433594
End ...
The time is 04:20:45pm
0.26506042480469
The time is 04:20:45pm
2.3051910400391
0 - 2.3052673339844
1 - 2.3052215576172
2 - 2.3052215576172
3 - 2.3052215576172
4 - 2.3052215576172
5 - 2.3052215576172
6 - 2.3052215576172
7 - 2.3052215576172
8 - 2.3052215576172
9 - 2.3052215576172
End ...
The time is 04:20:56pm
在这里,您可以看到,它是由Apache一个接一个地处理的(查看时间),而且——因为我将代码封装在一个类中——内存使用率高达2.3MB

我认为这与PHP内存限制(设置为128MB)或Apache并发请求允许分配的内存量有关,或者可能与操作系统(Ubuntu)中的某些内容有关?限制PHP框架可以分配的内存量

我将Apache的MaxClient设置为一个高得离谱的数字,以确保它与此无关


有什么想法我应该在哪里寻找和重新设置吗?

只是一个预感:您是否在每个请求中启动了一个PHP会话(显式使用或无意中启用了INI设置),而您没有在早期的测试中启动?PHP的会话数据被锁定,以防止并发写入,因此第二个脚本将在会话重新打开时等待,直到第一个脚本终止或调用@Matt Raines-即使如此,它为什么在第一次工作而不是在第二次工作?不,我不会在我的办公室里开始课程code@Asaf如果没有看到TestTask的代码,就无法判断。我已经编写了一个简单的版本,将代码封装在一个类方法中,它对内存使用或并发运行的能力没有明显的区别,因此它必须是作为一个类重写以外的东西,这会导致这两种行为。我的假设是一个框架或其他什么东西开始了会话,但似乎我找错了方向。。。