Node. js Lesson 3 (node. js asynchronous IO and event programming)

Source: Internet
Author: User

The biggest feature of zookeeper Node. js is the close integration of asynchronous I/O with events. This mode is very different from the traditional synchronous I/O linear programming approach, because the control flow depends largely on events and callback functions, A logic is split into several cells. Contents: blocking and thread 1. synchronous I/O or blocking I/O threads usually take a long time if they encounter disk read/write or network communication during execution. At this time, the operating system will deprive the CPU of the thread of control, so that it pause the execution, and give the resource to other working threads. This thread scheduling method becomes blocked. When the I/O operation is completed, the operating system removes the thread's blocking status, restores its control over the CPU, and enables it to continue execution. 2. asynchronous I/O or non-blocking I/O does not use blocking policies for all I/O operations. When a thread encounters an I/O operation, it does not wait for the completion of the I/O operation or the return of data in a blocking way. Instead, it only sends the IO request to the operating system and continues to execute the next statement. When the operating system completes the IO operation, the thread that notifies the thread that executes the IO operation in the form of an event. The thread will process this event at a specific time. To handle asynchronous IO, the thread must have an event loop and constantly check whether there are any unprocessed events, process them in sequence. 3. The difference between non-blocking mode and non-blocking mode: in non-blocking mode, a thread is always performing computing operations. The CPU core utilization of this thread is always 100%, and IO is notified by event. In blocking mode, multithreading often improves the system throughput, because one thread is congested and other threads are working. multithreading can prevent the CPU resources from being wasted by the blocked threads. Scheduling: the current job is executed in 5 minutes. differences between synchronous I/O and asynchronous I/O synchronous I/O (blocking) asynchronous I/O (non-blocking) with multi-thread throughput, single thread can achieve high throughput. With event segmentation and thread scheduling, multi-core CPU can be used for function division. With multi-core CPU, single thread can be bound it is difficult for single-core CPUs to make full use of CPU resources, weak Data Locality, small memory trajectory, strong Data Locality, linear programming thinking, not traditional programming thinking

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.