Node family - understanding of blocking and non blocking

Endl 2021-05-03 14:43:25
node family understanding blocking non

original text…

You should have some understanding of the concepts of blocking and non blocking :

Just to be brief , Most of the blocking is due to synchronous mode , Non blocking can be understood as asynchronous mode processing some time-consuming operations

Then again Node.js How to describe blocking and non blocking in ? The following is mainly about this problem :

Node What's blocking in ?

I/O Operation can be understood as the interaction with the system disk ( Data reading and writing ) Or network requests, etc

Blocking is everything else JS Code execution has to wait until the previous time-consuming I/O operation Or some network requests, etc . because Node There is an event loop to solve this problem , Let's say js In the process of re execution , The event loop has not been opened , In fact, it will cause congestion .

Actually in Node.js Under normal circumstances, there is also a blocking situation , as a result of Node Asynchronous processing for I/O The operation is friendly 【 Using the event loop 】, But for JavaScript There may be some CPU Intensive operation performance is relatively low . Some students may say , Why? CPU Intensive operation Node.js It can't be supported asynchronously ? Because you can understand these CPU Intensive operation , It's all synchronization code , Like a lot of for loop , Massive data computing, etc .

Node.js There are also some synchronization methods in the standard library of , Most of these methods are based on libuv To achieve the blocking effect .Node There are also some blocking methods in the native module of . But at the same time Node The corresponding asynchronous version of API.

What's up? libuv Well ? It's a multi platform, asynchronous I/O The library of operations . See the official website for details

Node What's non blocking in ?

First of all, think about not limited to Node How to achieve non blocking in the context of ( asynchronous ) Well ?

To put it simply, two :

1) You can open multiple threads to handle concurrent operations

2) The pattern of the event cycle , If there are asynchronous operations in the event queue , After the end of the asynchronous operation , Call the corresponding callback function to process the asynchronous return result

Node.js It's single threaded , as a result of :Node.js The outer layer is made up of JavaScript Realized ,JavaScript The interpretation of execution is through V8`` engine To do the .

since JS The execution of is single threaded , Then we can't be JS During the process of interpretation and execution, open another thread to interpret and execute , therefore Node.js It's the second way to achieve non blocking ( Asynchronous operations ) Of .

Be careful not to mix Node Blocked and non-blocked API


  1. Why did gitlab choose vue.js?
  2. HTTP-RPC: 轻量跨平台REST服务
  3. 继全面采用Node.js以后,PayPal分享大幅度踩坑GraphQL心得 - Mark Stuart
  4. vue组件化开发实战之滚动/轮播的实现
  5. Http-rpc: lightweight cross platform rest Service
  6. Following the full adoption of node.js, PayPal shares a great deal of graphql experience mark Stuart
  7. Implementation of rolling / carousel in Vue component development
  8. CSS是什么?这一篇全解,绝对有你想要的
  9. What is CSS? This is a complete solution, there is absolutely what you want
  10. 04-HTML5常用标签-HTML5极速入门
  11. 04-html5 common tags
  12. WEB前端全套零基础视频教程+软件2021最新编程视频
  13. Web front end full set of zero basic video tutorial + software 2021 latest programming video
  14. 使用Node, Mongo, React, Redux实现Token认证
  15. Using node, Mongo, react and Redux to realize token authentication
  16. 体面编码之CSS和HTML
  17. CSS and HTML for decent coding
  18. 使用Playwright基于多浏览器进行javascript自动化测试的简单教程- Applitools
  19. A simple tutorial for JavaScript automatic testing based on multi browser using playwright - applitools
  20. Minimum distance to target element
  21. 浅谈 React 中的 XSS 攻击
  22. XSS attack in react
  23. 自学前端教程整理,附不容错过的前端100篇文章合集
  24. Self taught front-end tutorial collation, with a collection of 100 front-end articles that can not be missed
  25. 使用OpenTracing跟踪Go中的HTTP请求延迟
  26. Using opentracing to track HTTP request latency in go
  27. Encapsulating databinding allows you to write less than 10000 lines of code
  28. 03-HTML5标签-HTML5极速入门
  29. 03-html5 tag-html5 quick start
  30. LayUI - 极易上手拿来即用的前端 UI 框架
  31. Layui - easy to use front end UI framework
  32. Interpretation of lodash source code (1)
  33. Why is the first parameter of node family callback error?
  34. 报告:JavaScript 开发者达1380 万,C#超越 PHP,Rust 增长最快
  35. Report: Javascript developers reach 13.8 million, C surpasses PHP, and rust grows fastest
  36. 小白前端入门笔记(10),怎么设置网站内部的超链接?
  37. How to set up hyperlinks inside the website?
  38. Using node and socket to realize online chat room
  39. The core competitiveness of Vue: data bidirectional binding
  40. React configuration agent
  41. CSS layout
  42. Application scenario explanation of Vue dynamic component
  43. Redux learning notes 04 -- using multiple reducers to manage data
  44. After three months of typescript writing, what have I learned?
  45. Node family - what is a callback?
  46. React -- a simple implementation of render & create element
  47. JS learning simple usage of jquery
  48. Seamless love
  49. 小白前端入门笔记(12),设置哑链接
  50. Small white front-end entry notes (12), set dumb links
  51. Vue2. X opens composition API and TSX
  52. Interview record and thinking of social recruitment for one and a half years (Alibaba, Tencent, baidu offer)
  53. Flex learning notes
  54. The most essential closure article in the eastern hemisphere
  55. 2021-05-03 hot news
  56. Sword finger offer -- reverse order pair in array (JS Implementation)
  57. Working process of scaffold
  58. Use decorator mode to strengthen your fetch
  59. [JS] scope (Introduction)
  60. Employment information statistics network (interface document)