fiber-interop

fiber-interop

Run ordinary blocking-style PHP code concurrently on PHP Fibers. The code stays plain PHP: no promises, no callbacks (a call returns its value, instead of taking a function to call later), no yield. It runs on phasync, Revolt, AMPHP, ReactPHP, Swoole or TrueAsync; you pick the event loop by installing one adapter package.

fiber-interop is young (0.x versions) and the Scope RFC for php-src is a draft, so the API may change before 1.0.

What it looks like

Four common situations. The code is the same on every adapter: each snippet is a file in examples/front, and the four files were run unchanged on phasync, Revolt, AMPHP and ReactPHP with identical output.

Three HTTP requests at once (Guzzle)

use function fiber\{run, go, await_all};

$client = fiber\guzzle\client(['base_uri' => 'https://api.example.com']);

[$orders, $customer, $stock] = run(function () use ($client) {
    $orders = go(fn() => (string) $client->get('/orders/42')->getBody());
    $customer = go(fn() => (string) $client->get('/customers/7')->getBody());
    $stock = go(fn() => (string) $client->get('/stock/A-100')->getBody());
    return await_all([$orders, $customer, $stock]);
});

$orders = json_decode($orders, true);
$customer = json_decode($customer, true);
$stock = json_decode($stock, true);

The three get() calls run in separate tasks and overlap; await_all() returns the three response bodies in the order given. If a task throws, await_all() lets every task settle and then rethrows the first exception, out of run().

Several database queries at once (PostgreSQL)

use function fiber\{run, go, await_all};

$rows = run(function () use ($dsn) {
    $query = function ($id) use ($dsn) {
        $c = fiber\pg\connect($dsn, PGSQL_CONNECT_FORCE_NEW);
        $r = fiber\pg\query_params($c, 'SELECT $1::int AS id, pg_sleep(0.3)', [$id]);
        return pg_fetch_assoc($r);
    };
    $a = go(fn() => $query(1));
    $b = go(fn() => $query(2));
    $c = go(fn() => $query(3));
    return await_all([$a, $b, $c]);
});

Three queries that each take 0.3 s finish in 0.3 s in total (local PostgreSQL, same on phasync and Revolt). One connection per task, because a connection runs one query at a time.

A timeout, and a sleep that holds nobody up

use function fiber\{run, go, await, sleep};
use fiber\TimeoutException;

run(function () {
    $ticker = go(function () {
        for ($i = 1; $i <= 3; $i++) {
            sleep(0.05);
            echo "tick $i\n";
        }
    });
    $slow = go(function () {
        sleep(5);
        return 'slow result';
    });
    try {
        echo await($slow, 0.2), "\n";
    } catch (TimeoutException) {
        echo "gave up after 0.2 s; the ticker kept running\n";
    }
    await($ticker);
});

await($task, 0.2) throws TimeoutException when the task is not done in time (the task itself keeps running). fiber\sleep() pauses only the calling task, so the ticker keeps ticking.

For library authors: one function, every runtime

use function fiber\{go, await_all, sleep};

function fetchAll(array $ms): array
{
    $tasks = [];
    foreach ($ms as $m) {
        $tasks[] = go(function () use ($m) {
            sleep($m / 1000);
            return "slept $m ms";
        });
    }
    return await_all($tasks);
}

The library depends on fiber-interop/api and imports no runtime. The application calls fiber\run(fn() => fetchAll([200, 200, 200])) and the three 200 ms sleeps take 0.2 s on whichever adapter is installed. See Library authors.

The API

Eight functions cover most code; the API reference has signatures and a runnable example for each.

This works in a controller, a command, a queue job or a service, in Laravel, Symfony, Slim, CodeIgniter or plain PHP; fiber-interop does not replace the framework.

One request, one Scope

function session(): object              // one per request, in any fiber
{
    static $all;
    $all ??= new WeakMap();
    return $all[Scope::getCurrent()] ??= new stdClass();
}

// each request runs in its own Scope; session() never mixes them up
go(fn() => (new Scope)->run(handle(...), $request1));
go(fn() => (new Scope)->run(handle(...), $request2));

A request handled inside (new Scope)->run(...) has an identity that follows the code across fibers, so a registry like this, a container's Lifetime::Scoped services, the locale or the timezone become per request, with per-scope superglobals on top. Application authors without a framework get it directly, and framework authors can build on it instead of shipping a second "worker mode" runtime or a process per request. Library authors never see it: they call fiber\go() and the scope is carried along. Every runtime gets a global \Scope, the native extension or the polyfill. Runnable version: examples/front/scope.php, same output on phasync and Revolt. More on the Scope page and in scoped services.

Measured: overlapping waiting time

Ten requests to an endpoint that answers after 0.3 s Time
plain Guzzle client, one after another 3.02 s
side by side with run/go/await_all, phasync adapter 0.33 s
side by side with run/go/await_all, Revolt, AMPHP or ReactPHP adapter 0.32 s

Measured on 2026-10-14 with PHP 8.5.11 (CLI, NTS) on Linux, Intel Xeon E5-2697 v3, against a local php -S server with 16 workers; each number is the same in three runs. The sources are in examples/get-started. Swoole was not measured on this machine.

What overlaps and what blocks

Inside fiber\run(), with any adapter (details):

Outside fiber\run() each of them behaves exactly like the library it wraps, so adding them to an existing application breaks nothing.

These still block every task inside run(): PDO, file_get_contents('http://...'), native sleep()/usleep() (use fiber\sleep()), and DNS lookups, unless the phasync extension or Swoole's hooks make them cooperative.

Which adapter do I install?

The application installs fiber-interop/api and exactly one adapter; libraries depend on the api only. If your application already runs on an event loop, install that loop's adapter.

Revolt

Running on Revolt without AMPHP? Also the smallest dependency tree of all adapters: revolt/event-loop and nothing else.

fiber-interop/revolt

AMPHP

Running on AMPHP? Tasks and AMPHP futures await each other.

fiber-interop/amp

ReactPHP

Running on ReactPHP? Tasks and React promises await each other on React's loop.

fiber-interop/react

Swoole

Running on Swoole or OpenSwoole? Uses Swoole's coroutines and runtime hooks.

fiber-interop/swoole

TrueAsync

Running on TrueAsync? Tasks are TrueAsync coroutines.

fiber-interop/trueasync

Quick start

No event loop yet? phasync is a complete runtime with nothing else to configure, and its optional extension (MIT, bundled with phasync or pie install phasync/ext-phasync) also makes PHP's own blocking calls (sleep(), curl_exec()) cooperative.

fiber-interop/phasync

Constraints

Bootstrapping: one line per runtime

composer require fiber-interop/api fiber-interop/phasync fiber-interop/guzzle

One adapter installed is picked up automatically; with several, call fiber\setup(new FiberInterop\Revolt\Adapter()) once. See Get started.

Next

For runtime and framework authors

One adapter package per event loop, each checked by the same conformance suite:

Specification · Conformance matrix · Scope · GitHub organization