- 论坛徽章:
- 0
|
php popen实现多任务
Php代码- 1.$update_num = 1000;
- 2.$limit = 100;
- 3.$p_num = 0;
- 4.
- 5.$line_cmd = 'ps -ef | grep doit.php | grep -v grep | wc -l';
- 6.$exec_cmd = '/usr/local/php/bin/php doit.php %s & > /dev/null 2>&1';
- 7.
- 8.$t1 = microtime(1);
- 9.for($i = 0 ; $i < $update_num ; $i++){
- 10. if($p_num <=0){
- 11. $fp1 = popen($line_cmd,'r');
- 12. //current shell process numbers
- 13. $line = fread($fp1,512);
- 14. pclose($fp1);
- 15. //processes can be pushed
- 16. $p_num = $limit - $line;
- 17. if($p_num <= 0 ){
- 18. usleep(100);
- 19. }
- 20. }
- 21.
- 22. $p_num = $p_num -1;
- 23. $cmd = sprintf($exec_cmd,$i);
- 24. $fp = popen($cmd,'r');
- 25.
- 26. pclose($fp);
- 27.}
- 28.
- 29.$t2 = microtime(1);
- 30.echo round($t2 - $t1,2);
- $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代码- 1.sleep(2);
- 2.$fp = fopen('doit.txt','a');
- 3.fwrite($fp,$argv[1]."\n");
- 4.fclose($fp);
- 5.exit(0);
复制代码 |
|