Use of phppthreads multi-thread extension: a relatively stable example. Today I have studied the worker stackable combination method. I have written two forms. although I can work, there will always be problems with not listening to memory growth;
So I sent the code of the first method to the author, and he hasn't replied to me yet.
I finally gave up the combination of the two methods and used worker directly. I found that 582000 jobs have been executed, the memory has not increased, and the speed is basically stable. the code is as follows:
[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; $ 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 * 5; $ ct = 0; while ($ count --) {// for ($ target = 0; $ target <5; $ target ++) $ worker [] = 'data _'. (++ $ ct); usleep (rand (,);} // sleep (10); $ 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 (" worker \ n "); // var_dump ($ works );
Virtual running speed:
[Plain] view plain copy print?
Work Mermory used 0.250 mb ram, time: 2.141709 ==> 607000 Work Mermory used 0.250 mb ram, time: 1.721918 ==> 608000 Work Mermory used 0.250 mb ram, time: 1.858363 ==> 609000 Work Mermory used 0.250 mb ram, time: 1.734542 ==> 610000 Work Mermory used 0.250 mb ram, time: 1.819794 ==> 611000 Work Mermory used 0.250 mb ram, time: 1.847132 ==> 612000 Work Mermory used 0.250 mb ram, time: 1.740353 ==> 613000 Work Mermory used 0.250 mb ram, time: 1.628364 ==> 614000 Work Mermory used 0.250 mb ram, time: 1.731518 ==> 615000 Work Mermory used 0.250 mb ram, time: 1.730583 ==> 616000 Work Mermory used 0.250 mb ram, time: 1.825315 ==> 617000 Work Mermory used 0.250 mb ram, time: 1.762334 ==> 618000 Work Mermory used 0.250 mb ram, time: 1.842860 ==> 619000 Work Mermory used 0.250 mb ram, time: 1.732677 ==> 620000