Home > Backend Development > PHP Tutorial > Implement multitasking with php popen_PHP tutorial

Implement multitasking with php popen_PHP tutorial

WBOY
Release: 2016-07-13 17:49:33
Original
1210 people have browsed it

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); 
?> 


Php代码   
sleep(2); 
$fp = fopen('doit.txt','a'); 
fwrite($fp,$argv[1]."n"); 
fclose($fp); 
exit(0); 
?>  (fblww-0227)


摘自 网络营销

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478347.htmlTechArticlePhp代码 ?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 21;...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template