DEV Community

Cover image for What is PHP Fiber? Does PHP Fiber Really Give You Asynchronous Execution?
Mahfuzur Rahman
Mahfuzur Rahman

Posted on

What is PHP Fiber? Does PHP Fiber Really Give You Asynchronous Execution?

When PHP 8.1 introduced Fibers, many developers wondered if they were a solution to PHP’s long-standing limitation as a single-threaded, synchronous language. Could Fibers make PHP asynchronous, like JavaScript with its event loops or Node.js? The answer is subtle: Fibers don’t provide true asynchronous execution, but they are a powerful tool for managing tasks more efficiently. Let’s explore this concept in detail.

What Are PHP Fibers?

Fibers are a mechanism for implementing cooperative multitasking in PHP. They allow you to pause and resume specific parts of code without blocking the entire PHP process. Think of a Fiber as a specialized function that can “yield” control back to the main program and then continue execution from where it left off when requested.

Key Features of Fibers:

  • You can start, pause, and resume their execution.
  • They operate within the same PHP process and do not introduce multi-threading.
  • They are particularly useful for structuring non-blocking code.

What Happens When a Fiber Is Paused?

When a Fiber is paused using Fiber::suspend(), the control returns to the main PHP script. This means:

  • The main process is free to continue executing other parts of your program.
  • The Fiber’s execution is halted temporarily, waiting for a resume() call.

For example:

$fiber = new Fiber(function () {
    echo "Fiber started\n";
    Fiber::suspend();
    echo "Fiber resumed\n";
});

echo "Before Fiber\n";
$fiber->start();
echo "After Fiber Start\n";
$fiber->resume();
echo "After Fiber Resume\n";
Output:
Enter fullscreen mode Exit fullscreen mode
Before Fiber
Fiber started
After Fiber Start
Fiber resumed
After Fiber Resume
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • Fiber::suspend() pauses the Fiber. Execution shifts back to the main script after $fiber->start().
  • The main script continues running (“After Fiber Start”).
  • When resume() is called, the Fiber picks up where it left off and completes its task.

Does Resuming a Fiber Block the Main Process?

Yes, but only temporarily. When you call Fiber::resume(), the Fiber runs synchronously within the main PHP process. During this time:

  • Other parts of your script (or other Fibers) cannot execute until the Fiber completes or pauses again.
  • The execution of the Fiber is blocking because PHP remains single-threaded. For example:
$fiber = new Fiber(function () {
    echo "Processing Fiber...\n";
    sleep(2); // Simulates a blocking task
    echo "Fiber Done\n";
});

echo "Before Fiber\n";
$fiber->start();
echo "Between Fiber Start and Resume\n";
$fiber->resume();
echo "After Fiber\n";
Output:
Enter fullscreen mode Exit fullscreen mode
Before Fiber
Processing Fiber...
Fiber Done
Between Fiber Start and Resume
After Fiber
Enter fullscreen mode Exit fullscreen mode

Here, the Fiber blocks the main process during the sleep(2) call. So, while Fibers provide a way to structure code for efficiency, they don’t magically enable parallel or truly asynchronous execution.

How Are Fibers Still “Non-Blocking”?

The term “non-blocking” refers to how Fibers enable better task management, not parallel execution. A Fiber doesn’t block the main process while it’s paused; instead, control is handed back to the main script or an event loop.

This is particularly useful for libraries or frameworks that use event-driven architectures, like ReactPHP or Amp, where:

  • Long-running or waiting tasks (e.g., database queries, API calls) can be suspended.
  • Other tasks can continue running in the meantime.
  • Once the task is ready, the Fiber is resumed, and its execution continues.

Imagine you are a chef preparing multiple dishes:
You start cooking a dish but pause to wait for something to boil.
While waiting, you begin preparing another dish.
When the first dish is ready, you return to it and continue cooking.

Similarly, Fibers allows PHP to “pause” a task and return to it later without holding up the entire process.

Why Fibers Are Not Truly Asynchronous.

Unlike asynchronous programming in JavaScript or Node.js, where tasks can run in parallel using threads or an event loop, Fibers:

  • Execute synchronously within a single PHP process.
  • Provide cooperative multitasking by allowing the developer to manually control task suspension and resumption.

In other words:
Fibers don’t introduce parallelism (tasks still run one at a time).
They are a tool for managing and structuring non-blocking code more efficiently.
While PHP Fibers don’t make PHP truly asynchronous, they are a powerful addition to the language.

Top comments (0)