TaskGroup::__construct
(PHP 8.6+, True Async 1.0)
public TaskGroup::__construct(?int $concurrency = null, ?Async\Scope $scope = null)
Creates a new task group.
Parameters
- concurrency
- Maximum number of concurrently running coroutines.
null— no limit, all tasks are started immediately. When the limit is reached, new tasks are placed in a queue and started automatically when a slot becomes available. - scope
- Parent scope. TaskGroup creates a child scope for its coroutines.
null— the current scope is inherited.
Examples
Example #1 Without limits
<?php
use Async\TaskGroup;
$group = new TaskGroup();
$group->spawn(fn() => "task 1"); // starts immediately
$group->spawn(fn() => "task 2"); // starts immediately
$group->spawn(fn() => "task 3"); // starts immediately
Example #2 With concurrency limit
<?php
use Async\TaskGroup;
$group = new TaskGroup(concurrency: 2);
$group->spawn(fn() => "task 1"); // starts immediately
$group->spawn(fn() => "task 2"); // starts immediately
$group->spawn(fn() => "task 3"); // waits in queue
See Also
- TaskGroup::spawn — Add a task
- Scope — Coroutine lifecycle management