Why Fibers Exist and How to Use Them Effectively in PHP
The article explains the limitations of blocking I/O in traditional multi‑process servers, introduces Fibers as lightweight cooperative units, describes how to replace blocking calls with non‑blocking ones, and outlines key pitfalls such as global state and process‑affecting functions.
When an HTTP server handles a request, it typically runs the request handling code in a single execution flow and returns the result to the client. Although the business logic may run quickly, most of the response time is spent waiting for I/O operations such as file reads, database queries, or external API calls. If a request blocks on I/O, the process cannot handle other requests, leading to poor concurrency.
To avoid this, traditional servers spawn many processes (or threads) so that while one process waits for I/O, others can continue serving other clients. However, creating a separate process for each concurrent request can be resource‑intensive.
What is a Fiber?
Fibers provide a lighter‑weight alternative to processes. A Fiber can pause its execution when it reaches a blocking I/O operation and resume later, allowing the same process to interleave many Fibers. Instead of letting the whole process sleep, the runtime switches to another ready Fiber, and when the I/O becomes ready, the original Fiber is resumed.
To use Fibers, synchronous blocking I/O functions must be replaced with non‑blocking equivalents. In PHP, this can be done by setting file descriptors to non‑blocking mode with stream_set_blocking($fd, false). An event loop (e.g., stream_select or a library like libevent) monitors the descriptors and notifies when they are ready, at which point the associated Fiber is resumed.
A Fiber manager links file descriptors to their Fibers. When the event loop detects that a descriptor is ready, it retrieves the corresponding Fiber and resumes its execution, enabling a single process to handle many concurrent requests.
Things to Watch When Using Fibers
All I/O functions must be rewritten to be non‑blocking; otherwise the process will still block.
Avoid functions that change the process state (e.g., sleep, exit, die) because they affect all Fibers in the process.
Global, super‑global, and static variables are shared across Fibers; only local variables are isolated. This can cause data interference if not managed carefully.
In summary, to achieve concurrent request handling within a single process using Fibers, you need to replace blocking I/O with non‑blocking calls, avoid process‑state‑changing functions, and limit reliance on shared global state.
Source: 唐三浪学习时钟, original link https://www.zhihu.com/question/453755244/answer/63066757600 .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
