agussuroyo/async

A simple asynchronous process manager for PHP using pcntl.

Maintainers

Package info

github.com/agussuroyo/async

pkg:composer/agussuroyo/async

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2025-03-19 06:24 UTC

This package is auto-updated.

Last update: 2026-05-05 22:13:17 UTC


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.