PHP Classes

How to Implement PHP Asyncronous programming using PHP 8.1 Fibers - 6 minutes - Lately in PHP podcast episode 94

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement PHP ...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 387

Categories: PHP Tutorials, Lately in PHP Podcast, News

PHP 8.1 introduced a new feature called fibers, allowing developers to implement PHP asynchronous programming.

If you implement asynchronous programming, it is possible to make your PHP scripts execute multiple tasks in parallel, thus making those scripts faster.

Read this article, watch a 6-minute video, or listen to episode 94 of the Lately in PHP podcast to learn how to use PHP fibers, PHP Swoole, or ReactPHP to make your PHP scripts finish faster.




Loaded Article

In this article you can learn:

How to Implement PHP Asyncronous programming using PHP 8.1 Fibers

1. Previous Episode: Find MySQL Slow Queries by Activating the Slow Query Log

2. This Episode: How to Implement PHP Asyncronous programming using PHP 8.1 Fibers - 6 minutes - Lately in PHP Podcast Episode 94


Contents


Listen or download the podcast, RSS feed and subscribe in iTunes

Click on the Play button to listen now.

Introduction music obtained with permission from: http://spoti.fi/NCS

View Podcast in iTunes

Listen on Spotify
Listen on Spotify


Sound effects obtained with permission from: https://www.zapsplat.com/

In iTunes, use the Subscribe to Podcast... item of the Advanced menu, and then enter the URL above to subscribe to this podcast.

Watch the podcast video

See the Lately in PHP podcast play list on YouTube and Subscribe to this channel there.

Episode 94 Video

Play Video

What was said in the podcast

1. Introduction

Hello. Welcome to the Lately in PHP podcast.

This is a new episode in which we will be talking about PHP fibers. This is a new feature that was released with PHP 8.1, and it is useful to implement what we call asynchronous programming.

2. Benefits of Asynchronous Programming

There is this article in which we talk about what is asynchronous programming and its benefits.

In just a few sentences asynchronous  programming allows developers to execute multiple tasks in parallel and eventually in the scripts that use this kind of programming will be running faster.

3. Example Case of Application of Asynchronous Programming

One typical an example could be the case of a page that displays database query results.

And usually when you execute the query, it will have to wait until the database server is executing the query and does not return the results.

With asynchronous programming the PHP script could send a request to the database server and execute the query and not have to wait for the query to finish.

In this case, the PHP script could do some other task like, for instance, processing the templates that will be used to render the database query results on the page.

This way the script can do two tasks in parallel, like the database query and the processing of the template that is going to be used to render the results.

The benefit, as we can see in this article is that your code will end up running faster. So this is the main benefit of asynchronous programming.

4. PHP Fibers Extension

With fibers that were released in in PHP 8.1, there is an extension called fibers, and it has several functions you can check this documentation page there is a basic example of how to implement a fiber.

Usually, this is more complex. We are not going to cover this in this episode. It's just to give you an idea of what you can do with fibers.

There is also, a class called Fiber allows you to set up some code that will be used to run one or more fibers that will manage the execution of multiple tasks in parallel. You can start a fiber. You can pause the fiber resume and even terminate.

In this article, I also talk about how to take advantage of PHP 8.1 fibers or specific PHP frameworks and extensions that use asynchronous programming.

5. PHP Swoole Extension

Besides the fibers that we already talked, there is the Swoole extension that provides functions that can execute many common tasks that may take some time to execute like for instance, reading and writing files and accessing network or database servers.

So Swoole provides these functions. Maybe it can use together with the fiber class to implement asynchronous programming.

6. ReactPHP Framework

If you don't want to learn all this kind of functions that you need you to to implement asynchronous programming you can also use the ReactPHP framework and this implements what we call event-driven programming.

It allows to define events that will be called when a certain the task is finished, and it also implements several aspects that are implemented with asynchronous programming.

7. Conclusion

So basically, there are several ways to implement asynchronous programming PHP. It's up to you to choose what one that is best for you if you want to take advantage of the eventual speed gains.

Well, with this, we end this episode of the Lately, in the PHP podcast, I hope you have enjoyed and join us next episode, in which we will be talking more about features of the latest PHP versions as well as features that are planned for future PHP versions that will be interesting and are already being planned and implemented.

That's all for now thank you for watching. Hope to see you on the next episode of the Lately in PHP podcast.

Show notes




You need to be a registered user or login to post a comment

1,615,559 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement PHP ...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)