I have a utility function used for executing a program through CLI (cmd, bash etc). It returns an array of 3 items: STDOUT
, STDERR
and EXIT CODE
.
So far, it's been working nicely without issues. In fact, the problem I have with it doesn't really hinder it's functionality, but I'm concerned about performance.
The problem is that in certain cases, PHP runs the same command multiple times (3 times in my case), even if it was supposed to only do this once.
/**
* Executes a program and waits for it to finish, taking pipes into account.
* @param string $cmd Command line to execute, including any arguments.
* @param string $input Data for standard input.
* @param boolean $log Whether to log execution failures or not (defaults to true).
* @return array Array of "stdout", "stderr" and "return".
*/
public static function execute($cmd,$stdin=null,$log=true){
//static $once=true; if(!$once)die; $once=false;
$proc=proc_open($cmd, array(
0=>array('pipe','r'),
1=>array('pipe','w'),
2=>array('pipe','w') ), $pipes);
fwrite($pipes[0],$stdin); fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]); fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]); fclose($pipes[2]);
$return=proc_close($proc);
if($return!=0 && $log)
xlog('Error: Program execution returned failure.',$stdout,$stderr,$return);
return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}
Note the commented line (line 9). That was for testing. I enabled it to ensure the target program only runs once (I was thinking my code may be calling the same function somehow). But even with that line enabled, the program still ran multiple times.
As it is, I have 2 places in my code where I'm executing the same program (on different occasions). The command line is the same for both of them.
However, on one occasion, the program runs once, while in the occasion, PHP runs the program 3 times.
I've been monitoring and seeing this behavior under Process Explorer. I'm using Windows 7 x64. The program is 32 bit, as is PHP.
Edit: The program in question is custom developed, and it doesn't open new processes.
It is very strange. And It is very hard to figure out without the full code.
If you are stressing your server calling the same page several times, my best bet it is probably related to the CPU round robin process. The PHP does not have time to set the static variable to false because at the same there are an another request to this method. Other possibility is the PHP is failing to isolate properly the static value and the different request to this method can read different memory position until the PHP sinchronize the values.