TaskSet::getIterator
(PHP 8.6+, True Async 1.0)
public TaskSet::getIterator(): Iterator
Returns an iterator that yields results as tasks complete.
TaskSet implements IteratorAggregate, so you can use foreach directly.
Each processed entry is automatically removed from the set, freeing memory
and decreasing count().
Iterator Behavior
foreachsuspends the current coroutine until the next result is available- The key is the same one assigned during
spawn()orspawnWithKey() - The value is an array
[mixed $result, ?Throwable $error]:- Success:
[$result, null] - Error:
[null, $error]
- Success:
- Iteration ends when the set is sealed and all tasks have been processed
- If the set is not sealed,
foreachsuspends waiting for new tasks
Important: Without calling
seal(), iteration will wait indefinitely.
Examples
Example #1 Streaming processing
<?php
use Async\TaskSet;
spawn(function() {
$set = new TaskSet(concurrency: 5);
for ($i = 0; $i < 100; $i++) {
$set->spawn(fn() => processItem($items[$i]));
}
$set->seal();
foreach ($set as $key => [$result, $error]) {
if ($error !== null) {
echo "Task $key: error — {$error->getMessage()}\n";
continue;
}
echo "Task $key: done\n";
// Entry removed, memory freed
}
echo $set->count() . "\n"; // 0
});
Example #2 Named keys
<?php
use Async\TaskSet;
spawn(function() {
$set = new TaskSet();
$set->spawnWithKey('users', fn() => fetchUsers());
$set->spawnWithKey('orders', fn() => fetchOrders());
$set->seal();
foreach ($set as $key => [$result, $error]) {
if ($error === null) {
echo "$key: received " . count($result) . " records\n";
}
}
});
See Also
- TaskSet::seal — Seal the set
- TaskSet::joinAll — Wait for all tasks
- TaskSet::joinNext — Next result