spawn_with
(PHP 8.6+, True Async 1.0)
spawn_with() — Launches a function in a new coroutine bound to the specified Scope or ScopeProvider.
Description
spawn_with(Async\ScopeProvider $provider, callable $task, mixed ...$args): Async\CoroutineCreates and starts a new coroutine in the Scope provided by $provider. This allows explicit control over which Scope the coroutine will run in.
Parameters
provider An object implementing the Async\ScopeProvider interface. Typically this is:
Async\Scope— directly, sinceScopeimplementsScopeProvider- A custom class implementing
ScopeProvider - An object implementing
SpawnStrategyfor lifecycle management
task A function or closure to execute in the coroutine.
args Optional parameters passed to task.
Return Values
Returns an Async\Coroutine object representing the launched coroutine.
Errors/Exceptions
Async\AsyncException— if the Scope is closed or cancelledTypeError— if$providerdoes not implementScopeProvider
Examples
Example #1 Launching in a specific Scope
<?php
use Async\Scope;
use function Async\spawn_with;
use function Async\await;
$scope = new Scope();
$c1 = spawn_with($scope, function() {
return file_get_contents('https://php.net');
});
$c2 = spawn_with($scope, function() {
return file_get_contents('https://github.com');
});
// Wait for all coroutines in the scope to complete
$scope->awaitCompletion();
?>Example #2 Inherited Scope
<?php
use Async\Scope;
use function Async\spawn_with;
$parentScope = new Scope();
$childScope = Scope::inherit($parentScope);
spawn_with($childScope, function() {
echo "Working in child Scope\n";
});
// Cancelling parent also cancels child
$parentScope->cancel();
?>Example #3 Using with ScopeProvider
<?php
use Async\Scope;
use Async\ScopeProvider;
use function Async\spawn_with;
class WorkerScope implements ScopeProvider
{
private Scope $scope;
public function __construct()
{
$this->scope = new Scope();
$this->scope->setExceptionHandler(function(\Throwable $e) {
error_log("Worker error: " . $e->getMessage());
});
}
public function provideScope(): Scope
{
return $this->scope;
}
public function shutdown(): void
{
$this->scope->disposeSafely();
}
}
$worker = new WorkerScope();
spawn_with($worker, function() {
// Working in a managed scope
});
$worker->shutdown();
?>Example #4 Passing arguments
<?php
use Async\Scope;
use function Async\spawn_with;
use function Async\await;
$scope = new Scope();
$coroutine = spawn_with($scope, function(string $url, int $timeout) {
// Use the passed arguments
return file_get_contents($url);
}, 'https://php.net', 5000);
$result = await($coroutine);
?>Notes
Note: If
ScopeProvider::provideScope()returnsnull, the coroutine is created in the current Scope.
Note: You cannot create a coroutine in a closed or cancelled Scope — an exception will be thrown.
See Also
- spawn() — launch a coroutine in the current Scope
- Scope — managing coroutine lifetimes
- Interfaces — ScopeProvider and SpawnStrategy