나는 다음과 같은 코드를 가지고 :PHP 프로세스 실행 시간 초과
/**
* 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 integer $timeout How much to wait from program in msecs (-1 to wait indefinitely).
* @return array Array of "stdout", "stderr" and "return".
*/
function execute($cmd,$stdin=null,$timeout=-1){
$proc=proc_open(
$cmd,
array(array('pipe','r'),array('pipe','w'),array('pipe','w')),
$pipes=null
);
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);
return array(
'stdout' => $stdout,
'stderr' => $stderr,
'return' => $return
);
}
그것은 두 개의 "문제"가 있습니다.
- 코드는 동기식입니다. 대상 프로세스가 닫힐 때까지 고정됩니다.
- 지금까지, 나는 "일시 정지 상관 없어
(예 : Windows에서 리눅스에 $cmd > /dev/null &
및 start /B $cmd
로) 명령의 다른 종류를 발행하지 않고"동결 "에서 할 수 없었습니다 ", 전혀. 난 단지 그 타임 아웃을 구현할 필요가있다.
참고 : 솔루션은 플랫폼 간 호환이 가능해야합니다. $cmd
을 변경하지 않아도된다는 점도 중요합니다. 복잡한 명령을 실행하고 있지만 문제가있을 수 있습니다. 그러나 수정 방법에 따라 다릅니다. , 나는 다른 대안을 선호한다.
은 내가 도울 수있는 몇 가지 자원을 발견했습니다
- Run perl file from PHP script but not wait for output on Windows Server
- PHP set timeout for script with system call, set_time_limit not working
- http://www.shapeshifter.se/2008/08/04/asynchronous-background-execution-with-php/