PHP code example of pfinalclub / asyncio-debug

1. Go to this page and download the library: Download pfinalclub/asyncio-debug library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pfinalclub / asyncio-debug example snippets




finalclub\AsyncioDebug\Debug\DebugRuntime;
use Pfinalclub\AsyncioDebug\Debug\FiberTracker;
use Pfinalclub\AsyncioDebug\Exporter\LogExporter;

// 启用调试运行时
DebugRuntime::enableRuntime();

// 设置日志导出器
DebugRuntime::getInstance()->setExporter(new LogExporter());

// 创建 Fiber 跟踪器
$tracker = new FiberTracker();

// 模拟 Fiber 生命周期
$fiberId = $tracker->onFiberCreated();
$tracker->onFiberStarted($fiberId);
$tracker->onFiberSuspended($fiberId, '等待 I/O');
$tracker->onFiberResumed($fiberId);
$tracker->onFiberTerminated($fiberId);

use Pfinalclub\AsyncioDebug\Asyncio\AsyncioBridge;

$bridge = AsyncioBridge::getInstance();

// 任务生命周期
$taskId = $bridge->onTaskSubmitted(fn() => 'Hello');
$bridge->onTaskStarted($taskId);
$bridge->onTaskCompleted($taskId, 'Hello World');

// Await 跟踪
$bridge->onAwaitEnter($fiberId, 'Http::get()');
// ... 等待操作 ...
$bridge->onAwaitExit($fiberId, 'Http::get()');

enum EventType: string {
    case FIBER_CREATED;
    case FIBER_STARTED;
    case FIBER_SUSPENDED;
    case FIBER_RESUMED;
    case FIBER_TERMINATED;

    case TASK_SUBMITTED;
    case TASK_STARTED;
    case TASK_COMPLETED;
    case TASK_FAILED;

    case AWAIT_ENTER;
    case AWAIT_EXIT;

    case LOOP_TICK;
}

$exporter = new LogExporter();
DebugRuntime::getInstance()->setExporter($exporter);

$exporter = new NullExporter();
DebugRuntime::getInstance()->setExporter($exporter);
bash
php examples/basic_usage.php     # 基础 Fiber 生命周期示例
php examples/async_tasks.php      # 异步任务调试示例
php tests/DebugRuntimeTest.php    # 运行单元测试