Today, we study the cooperation method of worker stackable, write two kinds of forms, although can work, but will appear the problem of memory not listening to growth;
So the first method of the code message to the author, and now he did not reply to me.
I finally gave up the two-way, directly using the worker, found that now performed 582,000 jobs, the memory does not grow, the speed is basically stable, the code is as follows:
[PHP]View Plain copy print?
- <?php
- ini_set(' zend.enable_gc ', true);
-
- class exampleworker extends Worker {
- public $stack _count = 0;
- Public Function __construct () {}
- Public Function run () {
- sleep (1); //waiting for Data
- $count = 0;
- $data _flag = true;
- $stime = Microtime (true);
- While (1) {
- if($data = $this->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 c21>));
- $len = floor(strlen($s)/2 );
-
- For ($i=0; $i < $len ; $i ++) {
- $tmp = $s[$i];
- $idx = intval($len-$i-1 );
-
- $s[$i] = $s[$idx];
- $s[$idx] = $tmp;
- }
- }
- }
-
- $worker= new exampleworker (sprintf ("worker [%d]" , 0));
- $worker->start ();
-
- $stime = Microtime (true);
- $count = 3000000; //99999999;
- $total = $count;
-
- $ct = 0;
- While ($count-) {
- //for ($target = 0; $target < 5; $target + +)
- $worker[]=' Data_ '. + +$ct);
- Usleep (rand (200,1800));
- }
- //sleep (ten);
- $runtime = (Microtime (True)-$stime);
- printf ("---------------------------------------------------------\ n");
- printf ("used time is%f \ n", $runtime);
- printf ("mermory used%.3FMB RAM, added%d, left%d\n", Memory_get_peak_usage (True)/1048576, $ Total, count($worker));
- printf ("---------------------------------------------------------\ n");
-
- //var_dump ($works);
Running speed on virtual:
[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
PHP pthreads use of multithreaded extensions: a more stable example.