官术网_书友最值得收藏!

Blocking and non-blocking examples using Terminal

Let's run these examples in real time and see what we get. And we have the two files (blocking and non-blocking files) that we saw in the previous section.

We'll run both of these files, and I'm using the Atom editor to edit my text files. These are things we'll be setting up later in the section, this is just for your viewing purpose, you don't need to run these files.

Now, the blocking and non-blocking files, will both get run and they'll do similar things to those we did in the previous section, just in a different way. Both use I/O operations, getUserSync and getUser, that take five seconds apiece. The time is no different, it's just the order they execute in that makes the non-blocking version much quicker.

Now, to simulate and show how things work, I'll add a few console.log statements as shown in the following code example, console.log('starting user1'), console.log('starting user2').

This will let us visualize how things work inside Terminal. By running node blocking.js, this is how we run files. We type node and we specify the filename, as shown in the following code:

 node blocking.js 

When I run the file, we get some output. starting user1 prints to the screen and then it sits there:

Now, we have the user1 object printing to the screen with the name Andrew, and starting user2 prints to the screen, as shown in the following code output:

After that, the user2 object comes back around five seconds later with the name of Jen.

As shown in the preceding screenshot, our two users have printed to the screen, and at the very end our sum, which is 3, prints to the screen; everything works great.

Notice that starting user1 was immediately followed by the finishing of user1, and starting user2 was immediately followed by the finishing of user2 because this is a blocking application.

Now, we'll run the non-blocking file, which I've called non-blocking.js. When I run this file, starting user1 prints, starting user2 prints, then the sum prints all back to back:

Around 5 seconds later, at basically the same time, user1 and user2 both print to the screen.

This is how non-blocking works. Just because we started an I/O operation doesn't mean we can't do other things, such as starting another one and printing some data to the screen, in this case just a number. This is the big difference, and this is what makes non-blocking apps so fantastic. They can do so many things at the exact same time without having to worry about the confusion of multi-threading applications.

Let's move back into the browser and take a look at those sentences again in the Node website:

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, and we saw that in action.

Because Node is non-blocking, we were able to cut down the time our application took by half. This non-blocking I/O makes our apps super quick, this is where the lightweight and efficient comes into play.

主站蜘蛛池模板: 自贡市| 洛南县| 比如县| 景东| 富源县| 志丹县| 富川| 巨鹿县| 屯留县| 大宁县| 天台县| 泰和县| 赤峰市| 噶尔县| 固安县| 普兰县| 封开县| 凤冈县| 重庆市| 金昌市| 仪征市| 介休市| 曲靖市| 和田县| 绵阳市| 仙居县| 四会市| 贡觉县| 石河子市| 正蓝旗| 高州市| 柯坪县| 城口县| 长丰县| 霍林郭勒市| 云阳县| 丰城市| 元阳县| 巴南区| 军事| 邵东县|