In-depth analysis of node. js asynchronous API and its limitations, in-depth analysis of node. jsapi

Source: Internet
Author: User
Tags install redis

In-depth analysis of node. js asynchronous API and its limitations, in-depth analysis of node. jsapi

Reasons for using asynchronous APIs

The concept of Asynchronization first becomes popular in Web because Javascript is executed on a single thread in the browser, and a thread is also shared with UI rendering. this means that UI rendering and response are stuck during Javascript execution. the Asynchronous Method (of course, in the so-called single-thread language) is adopted for better user experience without blocking the main thread to continue responding to user operations. this belongs to the scope of user experience.

Likewise, engineers with experience in other languages certainly understand that switching between threads consumes a lot of time (mainly switching between contexts and caching ), therefore, improving the efficiency is also the reason for using asynchronous APIs.

Of course, these are not absolutely correct, but they are all said by everyone. if the overhead of multi-thread creation is less than that of parallel execution, the multi-thread method is preferred, which is often considered a CPU-intensive processing task.

In short, asynchronous IO or asynchronous API can be regarded as a Node feature, because it is a platform for large-scale application of asynchronous IO at the application layer, it strives to allocate resources more efficiently in a single thread.

About Promise

Here, this article does not describe the usage of Promise in detail, but simply describes some APIs and trial scope of Promise:

// Combine the fs of nodejs. the readdir function creates a native Promisevar promiseTask = new Promise (function (resolve, reject) {fs. readdir ('/var/www', function (err, files) {if (! Err) {resolve (files) ;}else {reject (err) ;}}) ;}); promiseTask. then (function (files) {console. log ('content: '+ files); return files; // to demonstrate other APIs, you can continue to use then to define the next operation function after return .}); promiseTask. catch (function (err) {console. log ('error: '+ err );});

How to wait for multiple Promise to complete?

// PromiseTask. then (function (files) {var readFilsePromiseList = files. map (function (file, index) {return new Promise (function (resolve, reject) {fs. readFile (file, 'utf-8', function (err, str) {if (! Err) {resolve (str)} else {reject (err) }}) ;}); return Promise. all (readFilsePromiseList );}). then (function (fileStrArray) {console. log ('after reading the so-called file: '+ fileStrArray );});

This Code indeed shows the elegance of nodejs development.

So where is the problem?

Currently, the elegant language still relies on the operating system. That is to say, the system restrictions still exist:

I don't know if I can interpret this error as a file operation handle depletion, but I hope you can understand that the operating system cannot open unlimited files at the same time.

There are also:

This is easy to understand. Of course, the memory limit can be adjusted by adding the following two running parameters:

Node -- max-old-space-size = 8192./index. js # Unit: MB node -- max-new-space-size = 2048./index. js # Unit: KB

The above parameters take effect during V8 initialization. Once they take effect, they cannot be dynamically changed.

Many may propose that these two restrictions exist in the same language. Yes, they exist in the same language.

However, powerful GC or multi-threaded programming models in other languages allow engineers to release system resources in a timely manner after applying for them.

While nodejs can also manually release unnecessary system resources, can every operation in the referenced program be released in time?

Example:The redis package (npm install redis) of nodejs does not provide synchronous operation methods.

This means that more process control should be taken into account in the development process. Unfortunately, nodejs of the single-thread system is not good at this. It is precisely because there is no concept of multithreading and no lock mechanism in essence, it is also impossible to include the semaphore mechanism in the general sense. The result is that engineers do not know when to manually release resources.

Unless you have absolute control over your project, you do not use any third-party packages that use Asynchronous APIs.

Therefore, the current conclusion is that Promise is only a development technique. Understanding this is not applicable to all development scenarios.

Summary

The above is all about node. js asynchronous API and its limitations. I hope this article will help you. If you have any questions, you can leave a message.

Related Article

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.