AMPHP

AMPHP and Fiber-Based Concurrency

AMPHP (https://github.com/amphp/amp 4,432 ) v3 runs on fibers and the Revolt 932 (https://github.com/revoltphp/event-loop 932 ) loop: its functions return plain values and suspend the calling fiber while they wait, and async() returns a Future. composer require amphp/http-client installed amphp/amp 3.1.3, amphp/http-client 5.3.6 and Revolt 1.0.9:

Six concurrent fetches and a timer with AMPHP v3PHP
<?php
require __DIR__ . '/vendor/autoload.php';
use Amp\Http\Client\{HttpClientBuilder, Request};
use function Amp\{async, delay};
$client = HttpClientBuilder::buildDefault();
$get = fn (int $id): string =>                  // reads like blocking code; suspends a fiber
  $client->request(new Request("http://127.0.0.1:8323/?id=$id"))->getBody()->buffer();
$t0 = hrtime(true);
$futures = array_map(fn (int $id) => async($get, $id), range(1, 6));
$futures[] = async(function () use ($t0) {
  delay(0.2);
  printf("timer fired at %.2f s\n", (hrtime(true) - $t0) / 1e9);
});
$bodies = Amp\Future\await($futures);
printf("%d futures in %.2f s\n", count($bodies), (hrtime(true) - $t0) / 1e9);
Output
timer fired at 0.23 s
7 futures in 0.56 s

$get() reads like blocking code, yet six calls and a timer finished in about half a second: fibers let code run concurrently without callbacks. PDO is not fiber-aware; use amphp/mysql.