解析PHP实现多进程并行执行脚本

网络编程 2021-07-05 09:49www.168986.cn编程入门
本篇文章是对PHP实现多进程并行执行脚本进行了详细的分析介绍,需要的朋友参考下

由于php的进程是不支持多线程的,有些场景为了方便以及提高性能,可以用php实现多进程以弥补这个不足:

代码如下:

#!/usr/bin/env php
<?php
$cmds=array(
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',2),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',2),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php',2)
);

foreach($cmds as $cmd){
        $pid=ptl_fork();
        if($pid==-1){ //进程创建失败
                die('fork child process failure!');
        }
        else if($pid){ //父进程处理逻辑
                ptl_wait($status,WNOHANG);
        }
        else{ //子进程处理逻辑
                ptl_exec('/usr/local/bin/php',$cmd);
        }
}


Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by