承接 nunomaduro/pokio 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nunomaduro/pokio

Composer 安装命令:

composer require nunomaduro/pokio

包简介

Pokio is a dead simple asynchronous API for PHP that just works

README 文档

README

Overview Pokio

Caution: This package manipulates process lifecycles using low-level and potentially unsafe techniques such as FFI for inter-process communication, and preserving state across process spawns. It is intended strictly for internal use (e.g., performance optimizations in Pest). Don't use this in production or use at your own risk—no guarantees are provided.

Logo for pokio

Pokio

Build Status Total Downloads Latest Stable Version License YouTube Channel Subscribers

Pokio is a dead simple Asynchronous API for PHP that just works! Here is an example:

$promiseA = async(function () {
    sleep(2);
    
    return 'Task 1';
});

$promiseB = async(function () {
    sleep(2);
    
    return 'Task 2';
});

// just takes 2 seconds...
[$resA, $resB] = await([$promiseA, $promiseB]);

echo $resA; // Task 1
echo $resB; // Task 2

Behind-the-scenes, Pokio uses the PCNTL extension to fork the current process and run the given closure in a child process. This allows you to run multiple tasks concurrently, without blocking the main process.

Also, for communication between the parent and child processes, Pokio uses FFI to create a shared memory segment, which allows you to share data between processes fast and efficiently.

However, unlike other libraries, if PCNTL or FFI are not available, Pokio will automatically fall back to sequential execution, so you can still use it without any issues.

Installation

Requires PHP 8.3+.

⚡️ Get started by requiring the package using Composer:

composer require nunomaduro/pokio

Usage

  • async

The async global function returns a promise that will eventually resolve the value returned by the given closure.

$promise = async(function () {
    return 1 + 1;
});

var_dump(await($promise)); // int(2)

Similar to other promise libraries, Pokio allows you to chain methods to the promise (like then, catch, etc.).

The then method will be called when the promise resolves successfully. It accepts a closure that will receive the resolved value as its first argument.

$promise = async(fn (): int => 1 + 2)
    ->then(fn ($result): int => $result + 2)
    ->then(fn ($result): int => $result * 2);

$result = await($promise);
var_dump($result); // int(10)

Optionally, you may chain a catch method to the promise, which will be called if the given closure throws an exception.

$promise = async(function () {
    throw new Exception('Error');
})->catch(function (Throwable $e) {
    return 'Rescued: ' . $e->getMessage();
});

var_dump(await($promise)); // string(16) "Rescued: Error"

If you don't want to use the catch method, you can also use native try/catch block.

$promise = async(function () {
    throw new Exception('Error');
});

try {
    await($promise);
} catch (Throwable $e) {
    var_dump('Rescued: ' . $e->getMessage()); // string(16) "Rescued: Error"
}

Similar to the catch method, you may also chain a finally method to the promise, which will be called regardless of whether the promise resolves successfully or throws an exception.

$promise = async(function (): void {
    throw new RuntimeException('Exception 1');
})->finally(function () use (&$called): void {
    echo "Finally called\n";
});

If you return a promise from the closure, it will be awaited automatically.

$promise = async(function () {
    return async(function () {
        return 1 + 1;
    });
});

var_dump(await($promise)); // int(2)
  • await

The await global function will block the current process until the given promise resolves.

$promise = async(function () {
    sleep(2);
    
    return 1 + 1;
});

var_dump(await($promise)); // int(2)

You may also pass an array of promises to the await function, which will be awaited simultaneously.

$promiseA = async(function () {
    sleep(2);
    
    return 1 + 1;
});

$promiseB = async(function () {
    sleep(2);
    
    return 2 + 2;
});

var_dump(await([$promiseA, $promiseB])); // array(2) { [0]=> int(2) [1]=> int(4) }

Instead of using the await function, you can also invoke the promise directly, which will return the resolved value of the promise.

$promise = async(fn (): int => 1 + 2);

$result = $promise();

var_dump($result); // int(3)

Follow Nuno

License

Pokio was created by Nuno Maduro under the MIT license.

nunomaduro/pokio 适用场景与选型建议

nunomaduro/pokio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.64M 次下载、GitHub Stars 达 763, 最近一次更新时间为 2025 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「library」 「asynchronous」 「pokio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nunomaduro/pokio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nunomaduro/pokio 我们能提供哪些服务?
定制开发 / 二次开发

基于 nunomaduro/pokio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.64M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 763
  • 点击次数: 19
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 763
  • Watchers: 17
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-20