• PHP Video Tutorials

PHP - Pool::collect() Function



Pool::collect() function can collect references to completed tasks.

Syntax

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

Pool::collect() function can allow a pool to collect references determined to be garbage by an optionally given collector.

Pool::collect() function can return the number of remaining tasks in a pool to be collected.

Example

<?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
Advertisements