Coroutine::getId
(PHP 8.6+, True Async 1.0)
public Coroutine::getId(): int
Gibt den eindeutigen ganzzahligen Bezeichner der Coroutine zurueck. Der Bezeichner ist innerhalb des aktuellen PHP-Prozesses eindeutig.
Rueckgabewert
int – eindeutiger Coroutine-Bezeichner.
Beispiele
Beispiel #1 Grundlegende Verwendung
<?php
use function Async\spawn;
$coroutine1 = spawn(function() {
return "task 1";
});
$coroutine2 = spawn(function() {
return "task 2";
});
$id1 = $coroutine1->getId();
$id2 = $coroutine2->getId();
var_dump(is_int($id1)); // bool(true)
var_dump($id1 !== $id2); // bool(true)
Beispiel #2 Protokollierung mit Bezeichner
<?php
use function Async\spawn;
function loggedTask(string $name): \Async\Coroutine {
return spawn(function() use ($name) {
$id = \Async\current_coroutine()->getId();
echo "[coro:$id] Aufgabe '$name' gestartet\n";
\Async\delay(1000);
echo "[coro:$id] Aufgabe '$name' abgeschlossen\n";
});
}
Siehe auch
- Coroutine::getSpawnLocation – Erstellungsort der Coroutine
- current_coroutine() – Die aktuelle Coroutine abrufen