定制 agussuroyo/async 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

agussuroyo/async

最新稳定版本:1.0.0

Composer 安装命令:

composer require agussuroyo/async

包简介

A simple asynchronous process manager for PHP using pcntl.

README 文档

README

CI

Run PHP work in parallel by forking child processes. Each task returns a Future that resolves to the callback's return value — or rethrows its exception in the parent.

Requirements

  • PHP 7.4 – 8.4
  • pcntl extension

pcntl is unavailable on native Windows and disabled in most non-CLI SAPIs (php-fpm, mod_php). This library is for CLI scripts and long-running daemons.

Install

composer require agussuroyo/async

Quick start

use AgusSuroyo\Async\Async;

$async = new Async();

$future = $async->run(function () {
    return fetchPrice('SKU-1');
});

$price = $future->await(); // blocks until the child finishes

Concurrent fan-out

$async = new Async();

$futures = [];
foreach ($skus as $sku) {
    $futures[$sku] = $async->run(function () use ($sku) {
        return fetchPrice($sku);
    });
}

$prices = array_map(function ($f) { return $f->await(); }, $futures);

Children run in parallel up to the concurrency limit. Their await() order doesn't matter — each future resolves whenever its child finishes.

Errors propagate

$future = $async->run(function () {
    throw new \RuntimeException('boom');
});

try {
    $future->await();
} catch (\RuntimeException $e) {
    // 'boom' — child file/line/trace are appended to the message
}

If the original exception class's constructor isn't (string $message, int $code)-compatible, the parent rethrows as RuntimeException with the original class name embedded in the message. The child stack trace is preserved either way.

Concurrency limit

Default is CPU cores × 2. Override at construction or at runtime:

$async = new Async(4);  // at most 4 children at once
$async->max(8);         // change later

When the limit is reached, run() blocks until a slot frees up.

Fire-and-wait

If you don't need individual return values, ignore the futures and call wait():

foreach ($items as $item) {
    $async->run(function () use ($item) {
        process($item);
    });
}
$async->wait(); // blocks until every scheduled child has finished

Caveats

  • Return values must be serialize()-safe. They cross the fork via a Unix socket pipe. Resources (PDO connections, sockets, file handles) can't ride the channel — return scalars, arrays, or value objects.
  • No shared state across children. Each fork gets a copy-on-write snapshot of the parent's memory; mutations in the child stay in the child.
  • No event-loop integration. This is a cooperative scheduler that pumps inside wait() and Future::await(). Use a dedicated event loop (Revolt/Amp/ReactPHP) if you need async I/O.

Testing

docker compose up -d
docker compose exec php composer test
docker compose exec php composer analyse

License

MIT.

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固