我有一個用於通過CLI(cmd,bash等)執行程序的實用程序函數。它返回3個項目的數組:STDOUT
,STDERR
和EXIT CODE
。PHP proc_open多次打開
到目前爲止,它一直很好地工作沒有問題。事實上,我所遇到的問題並不妨礙它的功能,但我擔心的是性能。
問題是,在某些情況下,PHP多次運行相同的命令(在我的情況下是3次),即使它應該只做一次。
/**
* 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);
}
請注意註釋行(第9行)。那是爲了測試。我使它能夠確保目標程序只運行一次(我想我的代碼可能會以某種方式調用相同的函數)。 但即使啓用該行,該程序仍然運行多次。
事實上,我在我的代碼中執行相同的程序(在不同的場合)有兩個地方。兩個命令行都是相同的。
但是,有一次,該程序運行一次,而在這種情況下,PHP運行該程序3次。
我一直在Process Explorer下監視和看到這個行爲。我正在使用Windows 7 x64。該程序是32位的,就像PHP一樣。
編輯:有問題的程序是自定義開發的,它不會打開新的進程。
使用另一個進程的工具來驗證觀察。你沒有提到它是什麼程序(可能自己分成子進程)。 – mario 2011-01-23 21:10:22
@Christian:我們如何檢查它?正如你所說,你沒有提到它是什麼程序。馬里奧是完全正確的;你應該聽他的。 – 2011-01-23 23:09:09