current_coroutine

(PHP 8.6+, True Async 1.0)

current_coroutine() — Restituisce l’oggetto della coroutine attualmente in esecuzione.

Descrizione

current_coroutine(): Async\Coroutine

Valori di ritorno

Un oggetto Async\Coroutine che rappresenta la coroutine corrente.

Errori/Eccezioni

Async\AsyncException — se chiamata al di fuori di una coroutine.

Esempi

Esempio #1 Ottenere l’ID della coroutine

<?php
use function Async\spawn;
use function Async\current_coroutine;

spawn(function() {
    $coro = current_coroutine();
    echo "Coroutine #" . $coro->getId() . "\n";
});
?>

Esempio #2 Diagnostica

<?php
use function Async\spawn;
use function Async\current_coroutine;

spawn(function() {
    $coro = current_coroutine();

    echo "Creata da: " . $coro->getSpawnLocation() . "\n";
    echo "Stato: " . ($coro->isRunning() ? 'in esecuzione' : 'sospesa') . "\n";
});
?>

Vedi anche