백그라운드에서 (대기하지 않고) 일부 PHP 스크립트를 시작한 다음 모든 프로세스가 종료 될 때까지 기다려야합니다. 장애물과 비슷한 (멀티 스레딩에 익숙한 경우) 뭔가가 있어야합니다.PHP 스크립트 동기화
예 :
//...code...
run_script('myscript1.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript2.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript3.php');//it's like an exec but doesen't wait for the script to finish
do_something();
wait_until_all_proc_are_finished();//it will wait until all script are executed
do_something_else();
//....
내가 트릭을 할해야하는 스크립트를 생성, 내가 콘솔에서 테스트를하고 좋은 작동하지만 그것은 PHP 페이지에서 작업을 doesen't, 난 왜 이해가 안 돼요! 내가 코드를 여러 번 실행하면
class TSync{
private $threads=array();
function tcreate($p){
$tname=tempnam(null,'THS_');//create a temp name
$p=addslashes($p);//just to be sure
$name=addslashes($tname);
$ex= 'php -r "$fp=fopen(\''.$name.'\',\'r+\');flock($fp,LOCK_EX);include(\''.$p.'\');fclose($fp);"';
run_on_background($ex);//execute it on background
$this->threads[count($this->threads)]=$tname;
return count($this->threads)-1;//returns the thread "id"
}
function twait($id){
$f=$this->threads[$id];//recover the name
/***even this doesen't work***
$fp=fopen($f,'r');
flock($fp,LOCK_EX);
fclose($fp);
*/
echo date("H:i:s"),"#Locking on $f<br/>";
$ex= 'php -r "$fp=fopen(\''.$f.'\',\'r\');flock($fp,LOCK_EX);fclose($fp);"';
exec($ex);
unlink($f);
}
}
$t=new TSync();//create the class
$f=$t->tcreate(dirname(__FILE__).'/testth.php');//this is a test script that waits 10s
$t->twait($f);//now you should wait the script,commenting this line should result on the script not waiting
콘솔에 시작 유효 코드의 예
는 작동해야하므로 두 번째 스크립트는 첫 번째 대기start /b php.exe -r "$fp=fopen('C:\\Windows\\Temp\\THS6D7.tmp','w');flock($fp,LOCK_EX);include('C:/.../testth.php');fclose($fp);"
(창 테스트).
을 사용하여 문제를 해결할 수 있습니다. 정확히 무엇을 묻고 있습니까? –
(CLI가 아닌) 웹 페이지의 컨텍스트에서 사용할 때 왜 작동하지 않는지 묻는 질문 – Ben
벤이 맞습니다 : 게시 한 코드는 내가 물어야 할 것이지만 PHP에서는 작동하지 않습니다. 왜 그런지 안다; 문제를 해결하거나 다른 해결책을 게시 할 수 있다면 알려주세요. 어쨌든 나는 최근에 (5.3.0) fclose (php> 5.3.2 doesn't는 더 이상 그렇게하지 않는다)에서 자원의 잠금을 해제하고있는 것을 발견했다. – Plokko