Future::getCompletedLocation

(PHP 8.6+, True Async 1.0)

public function getCompletedLocation(): string

以格式化字符串的形式返回 Future 完成位置的信息。便于日志记录和调试。

返回值

string — 格式为 file:line 的字符串,例如 /app/worker.php:15。如果 Future 尚未完成,返回空字符串。

示例

示例 #1 获取字符串形式的完成位置

<?php

use Async\Future;
use Async\FutureState;

$state = new FutureState();
$future = new Future($state);

$state->complete("result");

echo $future->getCompletedLocation(); // /app/script.php:9

示例 #2 完整的 Future 生命周期追踪

<?php

use Async\Future;
use Async\FutureState;

$state = new FutureState();
$future = new Future($state);

\Async\async(function() use ($state) {
    \Async\delay(50);
    $state->complete("done");
});

$result = $future->await();

echo "Future lifecycle:\n";
echo "  Created at:   " . $future->getCreatedLocation() . "\n";
echo "  Completed at: " . $future->getCompletedLocation() . "\n";
echo "  Result:       " . $result . "\n";

参见