Pool::collect

(PECL pthreads >= 2.0.0)

Pool::collectCollect references to completed tasks

Beschreibung

public void Pool::collect ( Callable $collector )

Allows the Pool to collect references determined to be garbage by the given collector

Parameter-Liste

collector

A Callable collector

R├╝ckgabewerte

void

Beispiele

Beispiel #1 Creating Pools

<?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);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Hello from MyWork in Thread #140222468777728
object(Pool)#1 (6) {
  ["size":protected]=>
  int(8)
  ["class":protected]=>
  string(8) "MyWorker"
  ["workers":protected]=>
  array(1) {
    [0]=>
    object(MyWorker)#4 (1) {
      ["something"]=>
      object(Something)#5 (0) {
      }
    }
  }
  ["work":protected]=>
  array(0) {
  }
  ["ctor":protected]=>
  array(1) {
    [0]=>
    object(Something)#2 (0) {
    }
  }
  ["last":protected]=>
  int(1)
}

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top