note 118996 added to class.pool
| From: | meadowsjared at gmail dot com | Date: | Mon, 14 Mar 2016 19:16:55 +0000 |
| Subject: | note 118996 added to class.pool | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205161@lists.php.net to get a copy of this message | ||
Please note, when using the collect function, it's important that you extend the pool class so
you can keep checking for finished threads until they're all done.
<?php
class TestWork extends Threaded {
protected $complete;
//$pData is the data sent to your worker thread to do it's job.
public function __construct($pData){
//transfer all the variables to local variables
$this->complete = false;
$this->testData = $pData;
}
//This is where all of your work will be done.
public function run(){
usleep(2000000); //sleep 2 seconds to simulate a large job
$this->complete = true;
}
public function isGarbage() {
return $this->complete;
}
}
class ExamplePool extends Pool
{
public $data = array();
public function process()
{
// Run this loop as long as we have
// jobs in the pool
while (count($this->work)) {
$this->collect(function (TestWork $task) {
// If a task was marked as done
// collect its results
if ($task->isGarbage()) {
$tmpObj = new stdclass();
$tmpObj->complete = $task->complete;
//this is how you get your completed data back out [accessed by $pool->process()]
$this->data[] = $tmpObj;
}
return $task->isGarbage();
});
}
// All jobs are done
// we can shutdown the pool
$this->shutdown();
return $this->data;
}
}
$pool = new ExamplePool(3);
$testData = 'asdf';
for($i=0;$i<5;$i++) {
$pool->submit(new TestWork($testData));
}
$retArr = $pool->process(); //get all of the results
echo '<pre>';
print_r($retArr); //return the array of results (and maybe errors)
echo '</pre>';
?>
----
Server IP: 72.52.91.14
Probable Submitter: 104.190.192.27
----
Manual Page -- http://php.net/manual/en/class.pool.php
Edit -- https://master.php.net/note/edit/118996
Del: integrated -- https://master.php.net/note/delete/118996/integrated
Del: useless -- https://master.php.net/note/delete/118996/useless
Del: bad code -- https://master.php.net/note/delete/118996/bad+code
Del: spam -- https://master.php.net/note/delete/118996/spam
Del: non-english -- https://master.php.net/note/delete/118996/non-english
Del: in docs -- https://master.php.net/note/delete/118996/in+docs
Del: other reasons-- https://master.php.net/note/delete/118996
Reject -- https://master.php.net/note/reject/118996
Search -- https://master.php.net/manage/user-notes.php