In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what is the meaning of iUnip O in nodejs". The editor shows you the operation process through actual cases, the operation method is simple and fast, and it is practical. I hope this article "what is the meaning of Icano in nodejs" can help you solve the problem.
In nodejs, "Igambo O" means input and output, which usually means "Ihambo O" operation. "iUnip O" operation can be divided into single-thread serial execution and multi-thread parallel execution, single-thread installation sequential execution, any one of which will lead to code blocking in subsequent execution.
This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.
What is the meaning of Icano in nodejs
Many people are familiar with the Nginx server. Nginx is written in pure C and is used as a Web server. It has a good advantage in reverse proxy and load balancing services. Node is similar to Nginx servers in that they are event-driven.
In the browser, JavaScript executes on a single thread and shares a thread with UI rendering, so JavaScript rendering and response should be in a stagnant state during UI execution. (if the script executes for more than 100 milliseconds, the user will feel stuttered on the page.) When we encounter these situations, we will think of an asynchronous way to eliminate these waiting problems, without introducing the concepts of async and synchronization.
Next, let's take a look at the event-driven and non-blocking characteristics of NodeJS, which is of more far-reaching significance for us to better learn NodeJS development and build a high-performance Web platform.
Overview of 1.I/O operations:
The NodeJS O operation is no stranger to any developer, so let's talk briefly about the I.O operation of IMAGO. The Icano operation is divided into: single-thread serial execution and multi-thread parallel execution. These two methods have their own advantages and disadvantages. The cost of multithreading lies in the high cost of context switching between creating threads and executing threads, and multithreads are faced with the problems of lock and state synchronization. Single-threaded installation executes sequentially, and any slower execution will cause subsequent code execution to block. A description of the serial execution of a task (conceptually similar to synchronous execution) and the parallel execution of a task is as follows:
In NodeJS, single thread is used to keep away from deadlock and state synchronization problems, and asynchronous Imax O is used to keep single thread away from blocking in order to make better use of CPU. Asynchronous Iripple O expects that the call of Iamp O no longer blocks subsequent operations and allocates the original time waiting for Imax O to complete to other required services to execute.
In many cases, some developers are confused about the concepts of async / synchronization and blocking / non-blocking, and there is no connection between the two. Blocking Iswap O is called until all operations are completed at the kernel level of the system before the call ends. The non-blocking Iswap O is returned immediately after the call. The following figure is available for blocking and non-blocking Iripple O:
2.NodeJS asynchronous Icano resolution:
Event loop: when a process starts, Node creates a loop similar to while (true). The process of executing the loop body each time is called Tick, and the process of each Tick is to see if there is time to be processed.
Observer: there are one or more observers in each time cycle, and the process of determining whether there is an event to deal with is to ask these observers if they want to deal with another event.
Request object: there is an intermediate product in the transition from the invocation of JavaScript to the completion of the kernel's Ihop O operation, that is, the request object.
I / O thread pool: assemble the request, send it to the I / O thread pool to wait for execution, complete the first step of IWeiO operation, and enter the second part of callback notification. (in Windows, after the I _ result O operation in the thread pool is called, the obtained result is stored on the req- > result attribute, and then PostQueuedCompletionStatus () is called to notify IOCP that the current object operation has been completed. )
The following figure is shown in Asynchronous Iripple O:
3. NodeJS asynchronous programming example:
The related concepts of asynchronous Istroke O are introduced earlier. Here is an example of the operation of asynchronous Ithumb O:
Var config = require ('. / config.json'); var fs = require ("fs"); var http = require ('http'); var url_module = require ("url"); http.createServer (function (request, response) {var key= url_module.parse (request.url) .query.replace (' key=','') Switch (request.method) {case 'GET': / / Asynchronous Response Generation fs.readFile (config.dataPath + key,' utf8', function (err, value) {if (err) {/ / Return File Not Found if file hasn't yet been created response.writeHead (404, {'Content-Type':' text/plain'}) Response.end ("The file (" + config.dataPath + key + ") does not yet exist.");} else {/ / If the file exists, read it and return the sorted contents var sorted = value.split (config.sortSplitString). Sort (). Join (''); response.writeHead (200,{ 'Content-Type':' text/plain'}) Response.end (sorted);}}); break; case 'POST': / / Synchronously append POSTed data to a file var postData ='; request. On ('data', function (data) {postData + = data) ) .on ('end', function () {fs.appendFile (config.dataPath + key, postData, function (err) {if (err) {/ / Return error if unable to create/append to the file response.writeHead (400,{' Content-Type': 'text/plain'})) Response.end ('Error: Unable to write file:' + err);} else {/ / Write or append posted data to a file, return "success" response response.writeHead (200,{ 'Content-Type':' text/plain'}); response.end ('success') });}); break; default: response.writeHead (400,{ 'Content-Type':' text/plain'}); response.end ("Error: Bad HTTP method:" + request.method);}}) .subscription (config.serverPort); console.log ('synchronous server is running:', config.serverPort) This is the end of the content about "what is the meaning of iCompO in nodejs". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.