gpt4 book ai didi

php - PHP脚本同步

转载 作者:行者123 更新时间:2023-12-03 13:13:41 26 4
gpt4 key购买 nike

我需要在后台启动一些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页面上却不起作用,我不明白为什么!
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

在控制台上启动的有效代码示例(在Windows上测试)
start /b php.exe -r "$fp=fopen('C:\\Windows\\Temp\\THS6D7.tmp','w');flock($fp,LOCK_EX);include('C:/.../testth.php');fclose($fp);"

如果我多次启动该代码,则第二个脚本将等待第一个脚本运行,因此它应该可以工作。

最佳答案

您应该可以使用Gearman解决此问题

关于php - PHP脚本同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5601414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com