AsyncPool: Return whether there are tasks left to be collected from collectTasks()

this allows a while($pool->collectTasks()); style code.
This commit is contained in:
Dylan K. Taylor 2019-08-08 18:48:58 +01:00
parent bce126b6d0
commit a52e4f0392

View File

@ -225,8 +225,10 @@ class AsyncPool{
* Collects finished and/or crashed tasks from the workers, firing their on-completion hooks where appropriate. * Collects finished and/or crashed tasks from the workers, firing their on-completion hooks where appropriate.
* *
* @throws \ReflectionException * @throws \ReflectionException
* @return bool whether there are tasks left to be collected
*/ */
public function collectTasks() : void{ public function collectTasks() : bool{
$more = false;
foreach($this->taskQueues as $worker => $queue){ foreach($this->taskQueues as $worker => $queue){
$doGC = false; $doGC = false;
while(!$queue->isEmpty()){ while(!$queue->isEmpty()){
@ -254,6 +256,7 @@ class AsyncPool{
$task->onCompletion(); $task->onCompletion();
} }
}else{ }else{
$more = true;
break; //current task is still running, skip to next worker break; //current task is still running, skip to next worker
} }
} }
@ -261,6 +264,7 @@ class AsyncPool{
$this->workers[$worker]->collect(); $this->workers[$worker]->collect();
} }
} }
return $more;
} }
public function shutdownUnusedWorkers() : int{ public function shutdownUnusedWorkers() : int{