Home >Backend Development >PHP Tutorial >php pthreads 多线程扩展的使用:一个较为稳定例子。
今天研究了worker stackable的配合方法,写了两种形式,虽然能工作,但是都会出现内存不听增长的问题;
于是把第一个方法的代码邮件给了作者,到现在他没有回复我。
我最后放弃两者配合的方式,直接使用worker,发现到现在执行了582000个工作,内存没有增长,速度也基本稳定,代码如下:
[php] view plain copy print ?
shift()){ $data_flag = true; $this->doSomeWork(); if((++$count)%1000==0){ printf("Work Mermory used %.3fMB RAM, time: %3f===> %d \n", memory_get_peak_usage(true)/1048576, (microtime(true) - $stime), $count); $stime = microtime(true); } }else{ usleep(100000); } } exit; } private function doSomeWork(){ $str = 'sdalsadkfkasd;lfksa;ldfkas;lkf;lsadkf;as'; $len = strlen($str); $s = substr($str,rand(0,$len)); $len = floor(strlen($s)/2); for($i=0;$istart(); $stime = microtime(true); $count = 3000000;//99999999; $total = $count*5; $ct = 0; while($count--){ //for ($target = 0; $target
虚拟上的运行速度:
[plain] view plain copy print ?
Work Mermory used 0.250MB RAM, time: 2.141709===> 607000 Work Mermory used 0.250MB RAM, time: 1.721918===> 608000 Work Mermory used 0.250MB RAM, time: 1.858363===> 609000 Work Mermory used 0.250MB RAM, time: 1.734542===> 610000 Work Mermory used 0.250MB RAM, time: 1.819794===> 611000 Work Mermory used 0.250MB RAM, time: 1.847132===> 612000 Work Mermory used 0.250MB RAM, time: 1.740353===> 613000 Work Mermory used 0.250MB RAM, time: 1.628364===> 614000 Work Mermory used 0.250MB RAM, time: 1.731518===> 615000 Work Mermory used 0.250MB RAM, time: 1.730583===> 616000 Work Mermory used 0.250MB RAM, time: 1.825315===> 617000 Work Mermory used 0.250MB RAM, time: 1.762334===> 618000 Work Mermory used 0.250MB RAM, time: 1.842860===> 619000 Work Mermory used 0.250MB RAM, time: 1.732677===> 620000