DEV Community

Cover image for Asynchronous PHP: An Introduction to Fibers
JanWennrich
JanWennrich

Posted on • Edited on

Asynchronous PHP: An Introduction to Fibers

With the arrival of PHP 8.1, we've been introduced to a new and exciting feature that promises to transform the way we tackle asynchronous programming in PHP - Fibers. It's a change that not only simplifies how we write code but also drastically improves the efficiency of our applications.

Disclaimer: ChatGPT (GPT-4) was used to improve the wording of this post.

What Are Fibers?

First, let's get a basic understanding of Fibers. In computer science, a fiber is a mechanism that allows us to write asynchronous code in a more straightforward, synchronous-looking style. PHP 8.1 has added native support for fibers, which means we can now handle tasks like I/O operations, network requests, or database queries more efficiently.

Why Do Fibers Matter?

Fibers matter because they make asynchronous programming in PHP much more intuitive. Before fibers, if you wanted to write asynchronous PHP code, you'd have to rely on extensions and libraries.

Fibers, on the other hand, allow us to write asynchronous code that looks and feels like synchronous code. This makes our code easier to read, write, and maintain.

One of the biggest features of Fibers, is that they can be paused and resumed. Libraries like AMPHP use this to provide efficient parallel execution. We will take a closer look at this in a different post.

Working with Fibers: Some Tips

While fibers can simplify asynchronous PHP code, they also require you to think a bit differently about how you structure your code.

  1. Catch Exceptions: Fibers throw a FiberError exception when they fail, so be sure to catch these exceptions in your code.
  2. Be Careful with Shared State: Just like with threads, fibers can potentially lead to issues with shared state, especially if you're not careful. Make sure to properly manage shared resources to avoid race conditions.

Conclusion

Fibers represent a significant step forward for PHP, providing developers with a powerful tool to write efficient code natively. While they require a slightly different way of thinking about your code, the benefits they offer in terms of efficiency make them well worth considering for your next PHP project.

This post only scratched the surface of possibilities with Fibers. We will look at other features (like pausing and resuming execution) in another post.

Addendum

Check out the PHP manual for detailed information about Fibers:

Cover Photo by HΓ©ctor J. Rivas on Unsplash.

Top comments (5)

Collapse
 
ezimuel profile image
Enrico Zimuel • Edited

Fiber is not about parallelism. When you (or ChatGPT) said "a fiber is a lightweight thread of execution" this is not true. PHP runs always in one thread. Moreover, if you use sleep() that is a blocking call inside a Fiber you are not saving any time. If you run the code the total execution time is 30 sec (15 sec each). To be used properly the code inside a Fiber should be not blocking (e.g. curl_multi_exec). Please stop using ChatGPT to write contents without a human (expert) review. Moreover, you should put the advice "This post was written with the help of ChatGPT (GPT-4)" at the beginning and not at the end of the article to fully disclosure.

Collapse
 
leocavalcante profile image
Leo Cavalcante

Before fibers, if you wanted to write asynchronous PHP code, you'd have to rely on extensions like Swoole or ReactPHP. These are powerful tools, but they require you to write code in a somewhat convoluted, callback-oriented style.

Actually, Swoole uses Coroutines for its concurrency model, not callbacks.
And even with Fibers, you will still need a lib, like AMP, to achieve non-blocking I/O with Fibers, like @ezimuel pointed.

Collapse
 
klabauterjan profile image
JanWennrich

Thank you for your feedback, @leocavalcante, @ezimuel, @hsemix & @gerg0sz .

Your claims are absolutely right and I got to admit now that I have misunderstood how fibers really work. I'm sorry for spreading misinformation.

The example was based on my misunderstanding and not provided by ChatGPT. ChatGPT actually provided a correct but not as interesting example.

I updated the post by removing all false claims and putting a disclaimer about ChatGPT at the top of the article.

Collapse
 
gerg0sz profile image
Grzegorz K

The whole article is not truth. Please remove it or read more about Fibers

Collapse
 
hsemix profile image
Hamid Semix

I think both executions will take the same time (in this case 15s)