PHP脚本同步

发布于 2024-10-31 13:14:25 字数 2043 浏览 1 评论 0原文

我需要在后台启动一些 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);"

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

I need to launch some php script in background (without waiting)and then wait until all processes are terminated,something(if you are familiar with multithreading)similar to a barrier.

Example:

//...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();
//....

I created a script that should do the trick;i tested it in console and it works nice but it doesen't work on php pages,i don't understand why!

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

An example of the effective code launched on console (tested on windows)

start /b php.exe -r "$fp=fopen('C:\\Windows\\Temp\\THS6D7.tmp','w');flock($fp,LOCK_EX);include('C:/.../testth.php');fclose($fp);"

If i launch the code multiple times the second script will wait the first so it should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-11-07 13:14:25

您应该能够使用 Gearman 解决此问题

You should be able to solve this by using Gearman

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文