PHP code example of kode / runtime

1. Go to this page and download the library: Download kode/runtime 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/ */

    

kode / runtime example snippets


use Kode\Runtime\Runtime;

echo "当前运行环境: " . Runtime::getName();
// 输出: CONSOLE | SWOOLE | SWOW | FIBER | PROCESS | THREAD | CLI

use Kode\Runtime\Runtime;

Runtime::async(function () {
    echo "协程开始\n";
    Runtime::sleep(1.5);
    echo "协程结束\n";
});

echo "主流程继续执行\n";
Runtime::wait();

$channel = Runtime::createChannel(1);

Runtime::async(function () use ($channel) {
    $channel->push("Hello from coroutine");
});

Runtime::async(function () use ($channel) {
    Runtime::sleep(0.5);
    $data = $channel->pop();
    echo "收到: $data\n";
});

Runtime::wait();

Runtime::async(function () {
    $fp = fopen('/tmp/test.txt', 'w');

    Runtime::defer(function () use ($fp) {
        fclose($fp);
    });

    fwrite($fp, "Hello");
    Runtime::sleep(1);
});

Runtime::setEnvironment('process');

$pid = Runtime::fork(function () {
    echo "子进程 PID: " . getmypid() . "\n";
    Runtime::sleep(1);
});

Runtime::wait();

use Kode\Runtime\RuntimeCommand;
use Kode\Console\Input;
use Kode\Console\Output;

class AsyncTaskCommand extends RuntimeCommand
{
    public function __construct()
    {
        parent::__construct('async:task', '异步任务示例');
    }

    public function fire(Input $in, Output $out): int
    {
        $this->setOutput($out);

        $this->async(function () {
            $this->info('任务开始');
            $this->sleep(1);
            $this->success('任务完成');
        });

        $this->wait();
        return 0;
    }
}

final class Runtime
{
    // 获取运行时名称
    public static function getName(): string;

    // 异步执行
    public static function async(callable $callback): mixed;

    // 休眠
    public static function sleep(float $seconds): void;

    // 创建通道
    public static function createChannel(int $capacity = 0): ChannelInterface;

    // 注册清理回调
    public static function defer(callable $callback): void;

    // 等待完成
    public static function wait(): void;

    // Fork 进程
    public static function fork(callable $callback): int;

    // 设置环境
    public static function setEnvironment(string $environment): void;

    // 重置
    public static function reset(): void;
}

final class RuntimeAdapterFactory
{
    // 环境常量
    public const ENV_SWOOLE = 'swoole';
    public const ENV_SWOW = 'swow';
    public const ENV_FIBER = 'fiber';
    public const ENV_PROCESS = 'process';
    public const ENV_THREAD = 'thread';
    public const ENV_CLI = 'cli';
    public const ENV_CONSOLE = 'console';

    // 创建适配器
    public static function create(?string $environment = null): RuntimeInterface;

    // 强制创建指定环境适配器
    public static function createForEnvironment(string $environment): RuntimeInterface;

    // 检测方法
    public static function isSwooleAvailable(): bool;
    public static function isSwowAvailable(): bool;
    public static function isFiberSupported(): bool;
    public static function isConsoleAvailable(): bool;
}

abstract class RuntimeCommand extends \Kode\Console\Command
{
    // 异步执行
    protected function async(callable $callback): mixed;

    // 休眠
    protected function sleep(float $seconds): void;

    // 注册清理
    protected function defer(callable $callback): void;

    // 等待完成
    protected function wait(): void;

    // 创建通道
    protected function createChannel(int $capacity = 0): ChannelInterface;

    // 输出方法
    protected function info(string $message): void;
    protected function warn(string $message): void;
    protected function error(string $message): void;
    protected function success(string $message): void;
    protected function line(string $text, string $color = ''): void;
    protected function table(array $headers, array $rows): void;
    protected function progress(int $current, int $total, int $width = 50): void;
}

interface ChannelInterface
{
    public function push(mixed $data): bool;
    public function pop(): mixed;
    public function getCapacity(): int;
    public function getLength(): int;
    public function close(): void;
    public function isClosed(): bool;
}