note 74500 modified in function.popen by danbrown
| From: | danbrown@php.net | Date: | Mon, 02 Mar 2009 13:34:54 +0000 |
| Subject: | note 74500 modified in function.popen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-151324@lists.php.net to get a copy of this message | ||
Care needs to be taken in the case of long running child processes. Say you want to run tail -f
/var/log/messages or in my case burn dvds. If you have a busy wait, Apache2 can sit towards 100%cpu
and steadily grow memory. In my case I crashed the server after about an hour and 90% of the dvd
burned. During that time apache had consumed a gig of swap.
Offending code - don't copy:
<?php
$ThisCommand = sprintf("%s %s",COMMAND,$ThisFile);
$fp=popen($ThisCommand,"r");
while (!feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 4096);
if (strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while ($tok !== false) {
echo htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
}
pclose($fp);
?>
to go from zero memory and 100% cpu to negligible memory and negligible cpu add a sleep.
<?php
while (!feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 256);
if (strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while ($tok !== false) {
echo htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
// avoid a busy wait
sleep(1);
}
?>
I think the continued banging of the space to keep the browser awake triggered some issues in
apache.
--was--
Care needs to be taken in the case of long running child processes. Say you want to run tail -f
/var/log/messages or in my case burn dvds. If you have a busy wait, Apache2 can sit towards 100%cpu
and steadily grow memory. In my case I crashed the server after about an hour and 90% of the dvd
burned. During that time apache had consumed a gig of swap.
Offending code - don't copy:
$ThisCommand = sprintf("%s %s",COMMAND,$ThisFile);
$fp=popen($ThisCommand,"r");
while (!feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 4096);
if (strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while ($tok !== false) {
echo htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
}
pclose($fp);
to go from zero memory and 100% cpu to negligible memory and negligible cpu add a sleep.
while (!feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 256);
if (strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while ($tok !== false) {
echo htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
// avoid a busy wait
sleep(1);
}
I think the continued banging of the space to keep the browser awake triggered some issues in
apache.
http://php.net/manual/en/function.popen.php