spawn_with

(PHP 8.6+, True Async 1.0)

spawn_with() — Avvia una funzione in una nuova coroutine associata allo Scope o ScopeProvider specificato.

Descrizione

spawn_with(Async\ScopeProvider $provider, callable $task, mixed ...$args): Async\Coroutine

Crea e avvia una nuova coroutine nello Scope fornito da $provider. Questo consente un controllo esplicito su quale Scope eseguira la coroutine.

Parametri

provider Un oggetto che implementa l’interfaccia Async\ScopeProvider. Tipicamente questo è:

task Una funzione o closure da eseguire nella coroutine.

args Parametri opzionali passati a task.

Valori di ritorno

Restituisce un oggetto Async\Coroutine che rappresenta la coroutine avviata.

Errori/Eccezioni

Esempi

Esempio #1 Avvio in uno Scope specifico

<?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');
});

// Attendi il completamento di tutte le coroutine nello scope
$scope->awaitCompletion();
?>

Esempio #2 Scope ereditato

<?php
use Async\Scope;
use function Async\spawn_with;

$parentScope = new Scope();
$childScope = Scope::inherit($parentScope);

spawn_with($childScope, function() {
    echo "Lavoro nello Scope figlio\n";
});

// L'annullamento del genitore annulla anche il figlio
$parentScope->cancel();
?>

Esempio #3 Uso con 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("Errore worker: " . $e->getMessage());
        });
    }

    public function provideScope(): Scope
    {
        return $this->scope;
    }

    public function shutdown(): void
    {
        $this->scope->disposeSafely();
    }
}

$worker = new WorkerScope();

spawn_with($worker, function() {
    // Lavoro in uno scope gestito
});

$worker->shutdown();
?>

Esempio #4 Passaggio di argomenti

<?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) {
    // Usa gli argomenti passati
    return file_get_contents($url);
}, 'https://php.net', 5000);

$result = await($coroutine);
?>

Note

Nota: Se ScopeProvider::provideScope() restituisce null, la coroutine viene creata nello Scope corrente.

Nota: Non è possibile creare una coroutine in uno Scope chiuso o annullato — verrà lanciata un’eccezione.

Vedi anche