1 <?php 2 ini_set('memory_limit', -1); 3 require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; 4 5 6 $arr = range(0, 1000000); 7 8 class TestRunnable implements JennerSimpleForkRunnable 9 { 10 protected $data; 11 12 public function __construct(array $data) 13 { 14 $this->data = $data; 15 #$this->data[0] = 1; 16 } 17 18 /** 19 * @return void 20 */ 21 public function run() 22 { 23 sleep(10); 24 echo getmypid() . ':done当前内存'.(memory_get_usage()/2014/1024).'M' . PHP_EOL; 25 } 26 } 27 28 29 echo '当前内存'.(memory_get_usage()/1024/1024).'M'.PHP_EOL; 30 31 $pool = new JennerSimpleForkPool(); 32 $pool->execute(new JennerSimpleForkProcess(new TestRunnable($arr))); 33 $pool->execute(new JennerSimpleForkProcess(new TestRunnable($arr))); 34 $pool->execute(new JennerSimpleForkProcess(new TestRunnable($arr))); 35 36 $pool->wait();
注意利用fork进程写时复制特性节省内存
15行代码注释时,
当前内存32.427764892578M
1545:done当前内存16.514655225919M
1546:done当前内存16.515000465492M
1547:done当前内存16.515151750248M
15行代码未注释时,
当前内存32.428451538086M
1536:done当前内存32.787138468222M
1537:done当前内存49.05961783143M
1538:done当前内存65.331903239821M