Home » , » Let’s Have a Close Into Event Loops in PHP

Let’s Have a Close Into Event Loops in PHP

jaki watson | 04:32 | 0 comments
PHP Development Solutions
We all know that PHP developers are always waiting for something very interesting and innovative. Many a time, they wait for requests to remote services and sometimes, they wait for databases to return rows from a difficult query. What you think that whether it will be good if they could do other things while all that waiting?

Possibly you are familiar with callbacks and DOM events if you have written some JS. While professionals have callbacks in PHP, they do not work in quite the same way. Well, all thanks to a feature called the event loop.

Let’s have a look at how the event loop works and how we can make use of the event loop in PHP. First of all, we will look to see some of the interesting PHP libraries. Some of these consider them not yet stable enough to use in production. There are some people, who would consider the examples presented as better to do in more mature languages”.

There are a lot of good reasons to try these advanced things. However, there are various other good reasons to avoid such things in production. The main reason of this post is emphasizing what’s feasible in PHP.

Where Things Go To Wait

Let’s have a look at how they work in the browser to comprehend event loops. You can give a look at this example:

function fitToScreen(selector) {
var element = document.querySelector(selector);
var width = element.offsetWidth;
var height = element.offsetHeight;
var top = "-" + (height / 2) + "px";
var left = "-" + (width / 2) + "px";
var ratio = getRatio(width, height);
setStyles(element, {
"position": "absolute",
"left": "50%",
"top": "50%",
"margin": top + " 0 0 " + left,
"transform": "scale(" + ratio + ", " + ratio + ")"
});
}
function getRatio(width, height) {
return Math.min(
document.body.offsetWidth / width,
document.body.offsetHeight / height
);
}
function setStyles(element, styles) {
for (var key in styles) {
if (element.style.hasOwnProperty(key)) {
element.style[key] = styles[key];
}
}
}
fitToScreen(".welcome-screen");

When it comes to talking about this code, it does not require any extra libraries as it works in any browser, which supports CSS scale transformations. The current version of Chrome is all you need. You just need to ensure that the CSS selector well match with an element in your doc. These are few functions that take a CSS selector and center and scale the element in order to fit the screen. Do you know what will happen if any case we threw an Error inside that for loop?



In the above given image, you can see how PHP make use of a stack to store context. However, browsers are one-step ahead and deliver WebAPIs for things, including DOM events and Ajax callbacks. JavaScript is every bit as PHP. Both appear like they can perform numerous things at once, they are single threaded. They can only do one thing at a time. Developers are allowed to divest parallel work to different threads with the browser WebAPIs. Browsers are adding callbacks to a callback queue when all those things are happening.

Life Without An Event Loop
In JS, we are capable of running the following code:

setTimeout(function() {
console.log("inside the timeout");
}, 1);
console.log("outside the timeout");
Whenever we are running any of these codes, we can look at the outside the timeout and then inside the timeout in the console. The brwosers are allowing us to work with the setTimeout function which is the part of the WebAPIs. They can add the callback to the callback queue whenever 1 millisecond passed. Before the one from inside the setTimeout starts, the second console.log completes.

function setTimeout(callable $callback, $delay) {
$now = microtime(true);
while (true) {
if (microtime(true) - $now > $delay) {
$callback();
return;
}
}
}
setTimeout(function() {
print "inside the timeout";
}, 1);
print "outside the timeout";

Whenever we are running this, we can see inside the timeout and then outside the timeout. It is the reason we have to use an infinite loop inside setTimeout function in order to execute the callback after a delay. At some of the point, we can have a look at how we can do more compare to a single thing in a single thread at a time.

However, there is nothing like setTimeout in standard PHP as lots of obscure ways are there to implement non-blocking code along with event loops. We can make use of functions like stream_select to develop non-blocking network IO. To develop non-blocking filesystem code, we can make use of C extensions such as EIO. We can have a look at different libariries built on these unclear methods:

Icicle –

When it comes to Icicle, it is a library of components built with the event loop in mind. Let’s have a look at a simple example:

use Icicle\Loop;
Loop\timer(0.1, function() {
print "inside timer";
});
print "outside timer";
Loop\run();

The implementation of it is extremely wonderful as it comes with exclusive range of features like A+ promises, server implementations and socket.

ReactPHP –

ReactPHP comes with a similar event loop implementation; however, without interesting generation stuff:

$loop = React\EventLoop\Factory::create();
$loop->addTimer(0.1, function () {
print "inside timer";
});
print "outside timer";
$loop->run();
ReactPHP is extremely mature compare to Icicle and it comes with numerous components as well. Icicle has a way to go before it can contend with all the functionality ReactPHO provides. The developers are making good progress.
$loop = React\EventLoop\Factory::create();
$loop->addTimer(0.1, function () {
print "inside timer";
});
print "outside timer";
$loop->run();

ReactPHP is a lot more mature than Icicle and it has a range of components. Before it can contend with all the functionality React offers, Icicle has a way to go. The developers are making good progress.

Stay connected with us to get more information about it or we can get an assistant from professional PHP Developers regarding any of your queries.

Source Link : 
http://www.sitepoint.com/an-introduction-into-event-loops-in-php/
Share this article :

0 comments:

Post a Comment

 
Design by: