tags:

views:

871

answers:

4

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];
}
+2  A: 

Try closing the process each time after you write to it with pclose().

tkotitan
+3  A: 

You should close the pointer after using it with pclose.

Gumbo
A: 

popen can only return two things, either a resource or FALSE. Maybe you should test against FALSE instead of is_resource? You're leaking file handles, so the obvious thing to check for is making sure you're always closing a file once you open it, and the obvious place where you're opening file handles is the popen call. Trace through your logic and make sure you're not somehow skipping closing those pipes, either through a logic error or an exception.

Paul Tomblin
How do you close something that isn't a resource? In previous versions of this script, PHP threw a warning for trying to close something that wasn't a resource.
Jeremy DeGroot
How do you open it?
Paul Tomblin
I tried putting a pclose in an else for the is_resource check, and after the warnings started, it also started giving errors when I tried to close the non-resource pointers.
Jeremy DeGroot
A: 

I know you said get_load() uses file_get_contents(), but to make sure...does it properly close all the files it opens? Could you post the code from that function?

Edit: Even though it's a built-in function, try using something other than file_get_contents() to read the file and see what happens.

Michael Angstadt
`file_get_contents` is a build-in PHP function. It would be reckless if it doesn’t close the file after reading its content.
Gumbo