Coroutine::getId

(PHP 8.6+, True Async 1.0)

public Coroutine::getId(): int

코루틴의 고유 정수 식별자를 반환합니다. 식별자는 현재 PHP 프로세스 내에서 고유합니다.

반환값

int – 고유한 코루틴 식별자.

예제

예제 #1 기본 사용법

<?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)

예제 #2 식별자를 사용한 로깅

<?php

use function Async\spawn;

function loggedTask(string $name): \Async\Coroutine {
    return spawn(function() use ($name) {
        $id = \Async\current_coroutine()->getId();
        echo "[coro:$id] Task '$name' started\n";
        \Async\delay(1000);
        echo "[coro:$id] Task '$name' completed\n";
    });
}

같이 보기