PHP - Pool::collect() 函数



Pool::collect() 函数可以收集对已完成任务的引用。

语法

public int Pool::collect([ Callable $collector ] )

Pool::collect() 函数允许池收集由可选提供的收集器确定为垃圾的引用。

Pool::collect() 函数可以返回池中剩余待收集的任务数量。

示例

<?php
   class MyWork extends Stackable {
      public function __construct() {
         $this->complete = false;
      }
      public function run() {
         printf("Hello from %s in Thread #%lu\n", __CLASS__, $this->worker->getThreadId());
         $this->complete = true;
      }
      public function isComplete() { 
         return $this->complete; 
      }
      protected $complete;
   }
   class MyWorker extends Worker {
      public function __construct(Something $something) {
         $this->something = $something;
      }
      public function run() {
         /** ... **/
      }
   }
   $pool = new Pool(8, \MyWorker::class, [new Something()]);
   $pool->submit(new MyWork());
   usleep(1000);
   $pool->collect(function($work){
      return $work->isComplete();
   });
   var_dump($pool);
?>
php_function_reference.htm
广告