Skip to content

Lesson 5 of 9 · 24m

Fibers: Cooperative Concurrency Without Pretending PHP Has Threads

What fibers actually suspend, why they are infrastructure rather than application syntax, and how async PHP changes your mental model.

Fibers were announced and half the community heard “PHP threads now.” They are not threads, do not run in parallel, and do not make blocking database calls magically asynchronous. A fiber is a suspendable call stack. It lets a scheduler pause execution in the middle of a function and resume it later. That primitive makes async runtimes possible — and dangerous — in a language whose libraries were mostly written as blocking code.

The smallest useful example

$fiber = new Fiber(function (): void {
    echo "start
";
    Fiber::suspend("paused
");
    echo "resumed
";
});

$first = $fiber->start();
echo "main
";
$fiber->resume();

Output:

start
main
resumed

Control leaves the fiber at suspend, returns to the caller, then re-enters exactly where it left off. Local variables survive because the fiber owns a separate stack. This is coroutine machinery, not parallel execution.

Why frameworks care

An async runtime can start many fibers, suspend each one while waiting on I/O, and resume whichever socket becomes readable:

$server->run(function (Request $request): Response {
    $user = yield fetchUser($request->userId());       // conceptually suspended
    $posts = yield fetchPosts($user);                  // runtime resumes when ready
    return new Response(render('profile.php', compact('user', 'posts')));
});

The exact API differs between Amp, ReactPHP, Swoole, and OpenSwoole, but the shape is the point: one process can interleave thousands of waiting requests without one OS thread per connection. That is concurrency. Parallel CPU work still needs multiple processes, extensions that support it, or a different runtime model.

The blocking-library trap

Calling PDO's blocking query() inside a fiber does not suspend the fiber; it blocks the entire process. The scheduler cannot intercept arbitrary synchronous C code. Async PHP only works when the whole chain — HTTP client, database driver, filesystem, DNS — is nonblocking or offloaded. One innocent file_get_contents() can turn your event loop into a queue of stalled workers.

Application developers should usually consume fibers indirectly

You do not need to write new Fiber in a controller any more than you write epoll syscalls in a Laravel app. Learn the operational rules instead: never mutate shared request state without a plan, assume two requests interleave between awaits, use timeouts around every external call, and keep CPU-heavy work out of the event loop.

Fibers and exceptions

An exception thrown inside a fiber propagates out of start() or resume(). If nobody resumes it, the exception may vanish into a worker log or hang a promise depending on the runtime. In production async code, unhandled rejection handling and structured cancellation are not optional decorations; they are the failure model.

Field notes

  • Fibers cannot be serialized, cloned, or resumed after termination.
  • Fiber::suspend() outside a running fiber throws. Framework callbacks are usually inside one; random shutdown handlers may not be.
  • Async concurrency amplifies connection-pool exhaustion. Limit in-flight database operations explicitly.
  • Traditional PHP-FPM remains easier to operate for most applications. Choose an async runtime for I/O density, not fashion.
  • Measure event-loop delay. A median fast service can still have one JSON encode or image resize blocking every request behind it.

Was this lesson helpful?

Sign in to rate