PHP 8.5 async and performance: a practical advanced guide to Fibers

How to use Fibers correctly in PHP 8.5 for cooperative multitasking, including schedulers, non-blocking I/O, errors and performance limits.

PHP 8.5 async and performance: a practical advanced guide to Fibers

The title links PHP 8.5 and Fibers, but one correction matters: Fibers have existed since PHP 8.1. PHP 8.5 benefits from later maturity, but does not automatically turn PHP into an asynchronous runtime. A Fiber is an interruptible function with its own stack; useful concurrency also requires a scheduler, an event loop and genuinely non-blocking I/O.

What a Fiber does

A Fiber can suspend deep in the stack with Fiber::suspend(). The caller regains control and may activate it with resume() or inject an exception with throw(). After termination, its final value is available through getReturn().

<?php
$fiber = new Fiber(function (): string {
    $input = Fiber::suspend("waiting");
    return "received: " . $input;
});

echo $fiber->start();
echo $fiber->resume("data");
echo $fiber->getReturn();

start() returns the first suspended value; resume() returns the next suspended value or null on termination. Call getReturn() only after isTerminated().

Fibers are not Generators

A Generator suspends at explicit yield points and exposes a Generator return type. A Fiber suspends the current full stack, allowing intermediate functions to keep synchronous signatures. This transparency is valuable to async libraries, but suspension points still need careful control.

A minimal cooperative scheduler

This educational example alternates two Fibers. It does not run in parallel or perform I/O; it only demonstrates a cooperative queue.

<?php
$queue = new SplQueue();

function task(callable $job): Fiber {
    return new Fiber($job);
}

$queue->enqueue(task(function (): void {
    echo "A1\n";
    Fiber::suspend();
    echo "A2\n";
}));

$queue->enqueue(task(function (): void {
    echo "B1\n";
    Fiber::suspend();
    echo "B2\n";
}));

while (!$queue->isEmpty()) {
    $fiber = $queue->dequeue();
    $fiber->isStarted() ? $fiber->resume() : $fiber->start();
    if (!$fiber->isTerminated()) {
        $queue->enqueue($fiber);
    }
}

Each task must yield voluntarily. A long CPU-bound loop that never suspends blocks every other Fiber and the PHP process.

Blocking calls remain blocking

Putting file_get_contents() inside a Fiber does not make networking asynchronous:

<?php
$fiber = new Fiber(function (): void {
    $data = file_get_contents("https://example.com/api");
    Fiber::suspend($data);
});

The call occupies the thread until completion. Real async requires non-blocking sockets or a library that connects streams, timers and polling to an event loop. For production, prefer a mature ecosystem over a custom loop.

Exceptions, cancellation and state

Check isStarted(), isSuspended() and isTerminated(); resuming a non-suspended Fiber raises FiberError. Unhandled exceptions return to the caller of start() or resume(). Cancellation must be designed, for example by using throw() and releasing resources in finally.

Performance: where Fibers help

Fibers can improve throughput when many tasks wait for network, databases or timers and the I/O is genuinely non-blocking. They do not speed CPU-bound work or use multiple cores. Such workloads need processes, workers or dedicated extensions.

Measure latency, throughput, memory, simultaneous operations and timeout behaviour. Compare async with a simple synchronous implementation; at moderate traffic the extra complexity may not pay off.

Production checklist

  • require PHP 8.1+ and compatible dependencies;
  • use an event loop with truly non-blocking I/O;
  • control mutable shared state;
  • design timeouts, cancellation and backpressure;
  • close streams and connections in finally;
  • limit concurrency to protect databases and APIs;
  • profile with realistic load and observe failures.

In PHP 8.5, Fibers are a mature low-level cooperative multitasking tool, not magic async. Benefits come from correct integration with schedulers, event loops and non-blocking drivers.

Official sources

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0