Understanding event polling for node. js

Source: Internet
Author: User
Tags readfile sleep function

Objective

Overall :

    • Original Address: Understanding the event polling for node. js
    • Node Small app: Node-sample

A wise man reads a group of books. Also experience life

Two basic concepts of text node. js

The first basic concept of node. JS is that I/O operation overhead is huge:

So the biggest waste in the current technology is the wait for I/O operations to complete. There are several ways to address the impact of performance:

    • Synchronous mode : A processing request in the order of one one. Lee : simple. cons : No matter what one request can block all other requests.
    • Open a new process : Each request opens a new process. Pros : simple: a lot of links mean a lot of progress.
    • open a new thread : Each request opens a new thread. Lee : simple, and compared with the process. More friendly to the system kernel. Because the thread is much lighter than the process; cons : Not all machines support threads, and multithreaded programming can become too complex for situations where shared resources are to be handled.

The second basic concept is that each connection creates a new thread that is very memory-intensive (for example, you can recall the Apache memory exhaustion scenario against Nginx).

Apache is multithreaded: It opens a new thread (or process, depending on your configuration) for each request, when the number of concurrent connections increases. You can see how it runs out of memory at 1.1 points. Nginx and node. JS are not multi-threaded, because the thread consumption is too "heavy".

These two are single-threaded, event-based. This eliminates the thread/process consumption generated by processing many connections.

Single Thread

There is really just one thread: You can't run in parallel regardless of the code. For example: The following "Sleep" will clog sever1 seconds:

function sleep() {   varnew Data().getTime();   while (newDate1000) {         // do nothing   }}sleep();

But as far as I am concerned, I think a lot of people are mistaken about the so-called node single thread. In fact, the "single thread" given by the authorities is misleading. The so-called single-threaded means that your code runs on only one thread (many places call it the main thread). In fact, JavaScript's browser environment is not the same as the way we write JavaScript code, and many tasks of parallel processing, it is necessary to multi-threading, for example:

For example, the single thread in node. JS is referring to this primary thread. This main thread has a looping structure. Keep the whole program running (the code you wrote).

Event polling

In fact, what we said above is that the loop that maintains the main thread is "event polling", which exists in the main thread and is responsible for constantly invoking code written by the developer. However, it is not visible to developers. So ... How is the code written by the developer called? See:

For example, the asynchronous function is at the end of the run. An event is added to the event queue (following the FIFO principle), after the code in the main thread has finished running (that is, at the end of a loop), the next loop starts to "read" the event in the event queue, and then invokes the corresponding callback function (so the callback function is not necessarily in the order of operation). Assuming that the developer calls the blocking method in the callback function (for example, the sleep function above), the entire event polling is blocked and events in the event queue are not processed in a timely manner. Because of this, some of the library methods in Nodejs are asynchronous and encourage users to invoke asynchronous methods.

varrequire(‘fs‘);fs.readFile(‘hello.txt‘function (err, data) {  //异步读取文件  console.log("read file end");});while(1){    console.log("call readFile over");}

As the code above, although we use the Async method ReadFile to read the file, but read file end never output. Because the code is always in the while loop. The next event polling is never started, and it is impossible to ' read ' the event queue to invoke the corresponding callback function.

Finally, there is a node-sample that bloggers usually accumulate some code. including gaze, aggregated into a small application, or can see the clues of learning. Interested you can take a look.

Postscript

Articles:

    • Understanding the node. js Event Loop
    • Nodejs Event Polling Details

Understanding event polling for node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.