php popen实现多任务_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > php popen实现多任务

php popen实现多任务

 2011/10/28 8:17:34  qgymje  http://qgymje.iteye.com  我要评论(0)
  • 摘要:$update_num=1000;$limit=100;$p_num=0;$line_cmd='ps-ef|grepdoit.php|grep-vgrep|wc-l';$exec_cmd='/usr/local/php/bin/phpdoit.php%s&>/dev/null2>&1';$t1=microtime(1);for($i=0;$i<$update_num;$i++){if($p_num<=0){$fp1=popen($line_cmd,'r'
  • 标签:PHP 实现 多任务
$update_num = 1000;
$limit = 100;
$p_num = 0;

$line_cmd = 'ps -ef | grep doit.php | grep -v grep | wc -l';
$exec_cmd = '/usr/local/php/bin/php doit.php %s & > /dev/null 2>&1';

$t1 = microtime(1);
for($i = 0 ; $i < $update_num ; $i++){
        if($p_num <=0){
                $fp1 = popen($line_cmd,'r');
                //current shell process numbers
                $line = fread($fp1,512);
                pclose($fp1);
                //processes can be pushed
                $p_num = $limit - $line;
                if($p_num <= 0 ){
                        usleep(100);
                }
        }

        $p_num = $p_num -1;
        $cmd = sprintf($exec_cmd,$i);
        $fp = popen($cmd,'r');
        pclose($fp);
}

$t2 = microtime(1);
echo round($t2 - $t1,2);


sleep(2);
$fp = fopen('doit.txt','a');
fwrite($fp,$argv[1]."\n");
fclose($fp);
exit(0);
发表评论
用户名: 匿名