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.
run($fn): the entry point; runs$fnand returns its result when the tasks it awaits are done.go($fn): start a task, return an awaitable at once.await($x, $timeout): wait for one task, rethrow its exception;TimeoutExceptionon timeout.await_all($xs): wait for all; results under the same keys.await_any($xs): wait for the first to finish.sleep($s): pause this task only.readable($stream)/writable($stream): wait until a stream can be read or written.Scope: not a function but the other half of the API; one per request, so that anything keyed byScope::getCurrent()is per request.
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):
- Guzzle: requests from different tasks overlap (
fiber-interop/guzzle). - curl:
fiber\curl\exec()lets other tasks run while a transfer waits (fiber-interop/curl). - PostgreSQL:
fiber\pg\query()waits for the server without stalling the other tasks (fiber-interop/pg). - MySQL:
fiber\mysqli\query()waits for the server without stalling the other tasks (fiber-interop/mysqli). - Processes:
fiber\process\run()waits for a command while the other tasks continue (fiber-interop/process). - Redis: a Predis command suspends only the task that sent it (
fiber-interop/predis). - Doctrine DBAL: queries from different tasks overlap through the mysqli and pg drivers above (
fiber-interop/dbal).
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/revoltAMPHP
Running on AMPHP? Tasks and AMPHP futures await each other.
fiber-interop/ampReactPHP
Running on ReactPHP? Tasks and React promises await each other on React's loop.
fiber-interop/reactSwoole
Running on Swoole or OpenSwoole? Uses Swoole's coroutines and runtime hooks.
fiber-interop/swooleTrueAsync
Running on TrueAsync? Tasks are TrueAsync coroutines.
fiber-interop/trueasyncQuick 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/phasyncConstraints
- CPU-bound work does not get faster. Tasks take turns in one PHP process; only waiting overlaps.
- PostgreSQL and MySQL run one query at a time per connection, so concurrent tasks need one connection each: size connection pools for the number of tasks that query at once.
- Exceptions propagate:
await()rethrows a task's exception,await_all()rethrows the first one after every task has settled.
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
- Get started: which adapter, a first program you can time, the functions you need, and what blocks.
- Library authors: depend on
fiber-interop/apiinstead of one event loop. Guzzle is the worked example. - Containers: scoped services per request when requests run concurrently.
- API reference: every function of
fiber-interop/apiwith signature, behaviour and a runnable example. - Runtimes: Revolt, AMPHP, ReactPHP, Swoole, OpenSwoole, TrueAsync and phasync, with conformance status and notes.
- Integrations: every package above in detail.
For runtime and framework authors
One adapter package per event loop, each checked by the same conformance suite:
- phasync (
fiber-interop/phasync) - 25/25 conformance examples pass - Revolt (
fiber-interop/revolt) - 25/25 conformance examples pass - AMPHP (
fiber-interop/amp) - 25/25 conformance examples pass - ReactPHP (
fiber-interop/react) - 25/25 conformance examples pass - Swoole (
fiber-interop/swoole) - 25/25 conformance examples pass - TrueAsync (
fiber-interop/trueasync) - 25/25 conformance examples pass
Specification · Conformance matrix · Scope · GitHub organization