PHP 8.5.0 Alpha 4 available for testing

Voting

: min(two, three)?
(Example: nine)

The Note You're Voting On

olavk
10 years ago
Simple example with Collectable (basically Thread meant for Pool) and Pool

<?php

class job extends Collectable {
public
$val;

public function
__construct($val){
// init some properties
$this->val = $val;
}
public function
run(){
// do some work
$this->val = $this->val . file_get_contents('http://www.example.com/', null, null, 3, 20);
$this->setGarbage();
}
}

// At most 3 threads will work at once
$p = new Pool(3);

$tasks = array(
new
job('0'),
new
job('1'),
new
job('2'),
new
job('3'),
new
job('4'),
new
job('5'),
new
job('6'),
new
job('7'),
new
job('8'),
new
job('9'),
new
job('10'),
);
// Add tasks to pool queue
foreach ($tasks as $task) {
$p->submit($task);
}

// shutdown will wait for current queue to be completed
$p->shutdown();
// garbage collection check / read results
$p->collect(function($checkingTask){
echo
$checkingTask->val;
return
$checkingTask->isGarbage();
});

?>

<< Back to user notes page

To Top