• PHP Video Tutorials

PHP - Worker::collect() Function



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

Syntax

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

Worker::collect() function can allow a worker to collect references determined to be garbage by the optionally given collector.

Worker:: collect() function can return the number of remaining tasks on the worker's stack to be collected.

Example

<?php
   $worker = new Worker();
   echo "There are currently {$worker->collect()} tasks on the stack to be collected\n";

   for($i = 0; $i < 15; ++$i) {
      $worker->stack(new class extends Threaded {});
   }
   echo "There are {$worker->collect()} tasks remaining on the stack to be collected\n";

   $worker->start();
   while($worker->collect()); // blocks until all tasks have finished executing
   echo "There are now {$worker->collect()} tasks on the stack to be collected\n";
   $worker->shutdown();
?>
php_function_reference.htm
Advertisements