I'm running PHP 5.2 on Fedora, and I keep getting this warning after about 1000 iterations of my loop, which means the program has stopped working and needs to be restarted. I could set this up to exit after 1000 iterations and restart via a cron shortly thereafter, but that feels like the coward's way out. The loop follows; I should add that get_load()
preforms a file_get_contents()
call.
while ($row = select_row($sql))
{
while (($load = get_load()) > 10)
{
echo "Going to sleep (load: ".$load.")\n";
sleep(60*3);
}
$id = $row['id'];
foreach ($sizes as $abbr=>$size)
{
if($row[$size] != "yes")
{
continue;
}
$filename = "/images/".$abbr."/".$id.".jpg";
$tmp_file = "/tmp/".$id.".jpg";
if ($size == "large")
{
//We want to progressively interlace our large bookcovers because it saves on filesave above 10K.
$cmd = "convert -strip -interlace Plane ".$filename." ".$tmp_file;
}
else
{
$cmd = "convert -strip ".$filename." ".$tmp_file;
}
$convert = popen($cmd." 2>&1", "r");
if (is_resource($convert))
{
echo fgets($convert);
if(pclose($convert) == 0)
{
//Upload converted file to remote server
}
unlink($tmp_file);
}
}
Edit: After reading the first two answers, I realized that in taking out the file uploading code that wasn't relevant to my problem, I took out my pclose()
statement. Put in the pclose()
as it appears in my code.
Further edit: Posted get_load()
as requested
function get_load()
{
$load = explode(" ", file_get_contents("/proc/loadavg"));
return $load[0];
}