Nodejs asynchronous I/O and event programming

Source: Internet
Author: User

The biggest feature of node. JS is the programming pattern of asynchronous I/O(or non-blocking I/o) that is tightly coupled to events. This mode differs greatly from the traditional synchronous I/O linear programming approach, because the control flow is largely organized by events and callback functions, and a logic is split into several units

what is blocking (Block)? If the thread encounters disk read-write or network communication (collectively, I/O operation), usually takes a long time, and the operating system will deprive this thread of the CPU control so that it pauses execution while giving resources to other worker threads, a thread scheduling method called Blocking. When I/O when the operation is complete, the operating system relieves the blocking state of the thread and restores itsCPUcontrol, so that it continues to execute. This I/O pattern is the usual synchronous type I/O(synchronous I/O) or block-type I/O (Blocking I/O). 4

correspondingly, an asynchronous I/O (asynchronous I/O) or non-blocking I/O (non-blocking I/O) is for all I/O the operation does not adopt a blocking policy. When a thread encounters I/O operation, it will not wait in a blocked way I/O The completion of the operation or the return of the data, but only the I/O the request is sent to the operating system, and the next statement continues. When the operating system finishes I/O action, notify execution as an event I/O The thread that handles the event at a particular time. In order to handle asynchronous I/O, the thread must haveEvent Loops, and constantly check that there are no unhandled events, and then process them.

in blocking mode, a thread can only handle one task, and the throughput must be multithreaded if it is to be improved. Instead of blocking mode, a thread is always performing a compute operation, which is used by this thread CPU core utilization is always 100%, I/O notified by an event. In blocking mode, multithreading tends to improve system throughput because when one thread is blocked, other threads are working, and multithreading allows CPU resources are not wasted by threads that are blocked. In non-blocking mode, the thread is not I/O blocked, always in use CPU. The benefits of multithreading are simply the multi-core CPU in the case of the use of more nuclear, andnode. jssingle thread can also bring the same benefits. That's why node. js uses a single-threaded, non-blocking event programming pattern

callback function

Readfile.js

var fs = require (' FS ');
Fs.readfile (' file.txt ', ' utf-8 ', function(err, data) {

if (Err) {console.error (err);

} else {console.log (data);

} });

    Console.log (' end. ');

The results of the operation are as follows:

    End.    Contents of the file.
  I see you don't output the content until you output the end. Because it is asynchronous and non-blocking. function is a callback function.   node.js   i/o   

The work done by the Fs.readfile call only sends an asynchronous I/O request to the operating system, and then immediately returns and executes the following statement, which enters the event loop listener event after execution. when FS receives an event that the I/O request completes, the event loop actively invokes the callback function to complete the subsequent work. So we'll see end first. , and then see Contents of the File.txt file

node. js , not all of the API both synchronous and asynchronous versions are available. node. js use of synchronous IO is discouraged

node. js the event loop mechanism

node. js when will it go into the event loop? The answer is node. js The program starts with the event loop,loop ends, all logic is the callback function of the event, so node. js always in the event loop, the program entry isthe callback function for the first event in an event loop. The callback function of the event may be emitted during the execution of the I/O Request ordirectlyLaunch(Emit) event, and then return to the event loop after execution, the event loop checks that the event queue has nohandle the event until the end of the program

Modules and Packages

What is a module?

module is node.js The basic components of the application, the files and modules are one by one corresponding. In other words, a node.js file is a module, this file may be javascript code, JSON c/c++ extended

Creating and Loading modules

        


Nodejs asynchronous I/O and event programming

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.