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

Mahfuzur Rahman
4 min readNov 29, 2024

--

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:

Before Fiber
Fiber started
After Fiber Start
Fiber resumed
After Fiber Resume

Here’s what’s happening:

  1. Fiber::suspend() pauses the Fiber. Execution shifts back to the main script after $fiber->start().
  2. The main script continues running (“After Fiber Start”).
  3. 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:

Before Fiber
Processing Fiber...
Fiber Done
Between Fiber Start and Resume
After Fiber

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:

  1. You start cooking a dish but pause to wait for something to boil.
  2. While waiting, you begin preparing another dish.
  3. 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.

Event Loops and Fibers

To achieve practical non-blocking behavior, Fibers are often combined with event loops. An event loop tracks multiple tasks and decides which one to execute next. Here’s how it works:

  1. When a blocking task is encountered (e.g., a database query), the Fiber suspends itself.
  2. The event loop picks up another task to process in the meantime.
  3. When the blocking task is complete, the event loop resumes the corresponding Fiber.

Libraries like Amp and ReactPHP leverage Fibers and event loops to implement asynchronous task handling.

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. Here’s why:

  1. Improved Task Management: They allow you to pause and resume tasks, freeing the main process to handle other work in the meantime.
  2. Non-Blocking Workflows: Combined with event loops, Fibers enable non-blocking task handling.
  3. Synchronous Execution: When resumed, Fibers execute synchronously, temporarily blocking the process.

Fibers represent a step forward for PHP developers building applications that require efficient multitasking, particularly in areas like real-time applications, background jobs, or APIs. However, for true parallelism, you’ll still need external solutions like multi-threading via extensions or separate processes.

--

--

Mahfuzur Rahman
Mahfuzur Rahman

Written by Mahfuzur Rahman

Software Developer, Expertise in PHP, Laravel, JS, Vue, Nuxt, System Design, Database Design, DSA. Love learning and exploring new tech everyday.

No responses yet