26.3 Fibers
With Fibers, a new feature was introduced in PHP 8.1 to perform several tasks simultaneously. Strictly speaking, Fibers are functions with a pause button: if desired, they can be interrupted and resumed later. This also results in the three main methods of an instance of the Fiber class:
-
start()Starts a fiber function
-
suspend()Pauses a fiber function
-
resume()Resumes a paused fiber function
Why is this topic in a chapter that is primarily about network communication and HTTP? Because Fibers are a great way to perform parallel HTTP requests! To do this, we create several instances of the Fiber class. The constructor contains the code of the Fiber as an anonymous function. We then execute all Fibers in a loop by calling their ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access