PHP pthreads use of multithreaded extensions: a more stable example.

Source: Internet
Author: User
Tags rand usleep

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?
  1. <?php
  2. ini_set(' zend.enable_gc ', true);
  3.   
  4. class exampleworker extends Worker {
  5. public $stack _count = 0;
  6. Public Function __construct () {}
  7. Public Function run () {
  8. sleep (1); //waiting for Data   
  9. $count = 0;
  10. $data _flag = true;
  11. $stime = Microtime (true);
  12. While (1) {
  13. if($data = $this->shift ()) {
  14. $data _flag = true;
  15. $this->dosomework ();
  16. if((+ +$count)%1000==0) {
  17. printf ("work mermory used%.3FMB RAM, time:%3f===>%d \ n",
  18. Memory_get_peak_usage (True)/1048576, (Microtime (True)- $stime), $count  );
  19. $stime = Microtime (true);
  20.                 }  
  21. }Else{
  22. Usleep (100000);
  23.             }  
  24.         }         
  25. exit;
  26.     }  
  27. Private function dosomework () {
  28. $str = ' sdalsadkfkasd;lfksa;ldfkas;lkf;lsadkf;as ';
  29. $len = strlen($str);
  30.           
  31. $s = substr($str, rand (0,$len  c21>));
  32. $len = floor(strlen($s)/2  );
  33.           
  34. For ($i=0; $i < $len ; $i ++) {  
  35. $tmp = $s[$i];
  36. $idx = intval($len-$i-1  );
  37.   
  38. $s[$i] = $s[$idx];
  39. $s[$idx] = $tmp;
  40.         }  
  41.     }  
  42. }  
  43.   
  44. $worker= new exampleworker (sprintf ("worker [%d]"   , 0));
  45. $worker->start ();
  46.   
  47. $stime = Microtime (true);
  48. $count = 3000000; //99999999;   
  49. $total = $count;
  50.   
  51. $ct = 0;
  52. While ($count-) {
  53. //for ($target = 0; $target < 5; $target + +)
  54. $worker[]=' Data_ '.  + +$ct);
  55. Usleep (rand (200,1800));
  56. }  
  57. //sleep (ten);   
  58. $runtime = (Microtime (True)-$stime);
  59. printf ("---------------------------------------------------------\ n");
  60. printf ("used time is%f \ n", $runtime);
  61. printf ("mermory used%.3FMB RAM, added%d, left%d\n", Memory_get_peak_usage (True)/1048576, $  Total, count($worker));
  62. printf ("---------------------------------------------------------\ n");
  63.   
  64. //var_dump ($works);   


Running speed on virtual:

[Plain]View Plain copy print?
  1. Work Mermory used 0.250MB RAM, time:2.141709===> 607000
  2. Work Mermory used 0.250MB RAM, time:1.721918===> 608000
  3. Work Mermory used 0.250MB RAM, time:1.858363===> 609000
  4. Work Mermory used 0.250MB RAM, time:1.734542===> 610000
  5. Work Mermory used 0.250MB RAM, time:1.819794===> 611000
  6. Work Mermory used 0.250MB RAM, time:1.847132===> 612000
  7. Work Mermory used 0.250MB RAM, time:1.740353===> 613000
  8. Work Mermory used 0.250MB RAM, time:1.628364===> 614000
  9. Work Mermory used 0.250MB RAM, time:1.731518===> 615000
  10. Work Mermory used 0.250MB RAM, time:1.730583===> 616000
  11. Work Mermory used 0.250MB RAM, time:1.825315===> 617000
  12. Work Mermory used 0.250MB RAM, time:1.762334===> 618000
  13. Work Mermory used 0.250MB RAM, time:1.842860===> 619000
  14. Work Mermory used 0.250MB RAM, time:1.732677===> 620000

PHP pthreads use of multithreaded extensions: a more stable example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.