In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of node's multi-process and multi-threading. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Multi-process and Multi-thread in node.js
In node.js, the execution of javascript code is single-threaded, but Node itself is multithreaded.
Node itself is divided into three layers
The first layer, the Node.js standard library, is written by Javascript, that is, we use API that can be called directly during the process, which can be seen in the lib directory in the source code.
The second layer is Node bindings, which is the key to the communication between Javascript and the bottom layer Cmax. The former calls the latter through bindings and exchanges data with each other, which is the bridge between the first layer and the third layer.
The third layer, which is the key to supporting the operation of Node.js, is implemented by Cramble + and is some of the underlying logic implemented by node.
Among them, the third layer of Libuv, which provides Node.js with cross-platform, thread pool, event pool, asynchronous Icano and other capabilities, is the key to the power of Node.js.
Because Libuv provides event loop mechanism, javascript will not block in io processing, so when we use node to build web service, we do not need to worry about the excessive amount of io, which will lead to other request blocking.
However, the execution of non-io tasks, which is executed in the main node thread, is single-threaded. If there are time-consuming synchronous computing tasks, it will block the execution of other code.
Const Koa = require ('koa'); const app = new Koa (); app.use (async (ctx) = > {const url= ctx.request.url; if (url==='/') {ctx.body = {name: 'xxx', age: 14}} if (url==='/compute') {let sum=0 for (let I = 0; I {console.log (' http://localhost:4000/ start')})
In the above code, if http requests / compute, node will call cpu to do a lot of calculation, and if other http requests enter, it will block.
So how to solve this problem?
There are two solutions, one is to use children_process or cluster to open multi-processes for computing, and the other is to use worker_thread to open multi-threads for computing.
Multiprocess vs multithreading
Compare multithreading with multiprocess:
Attribute multi-process and multi-thread compare the complexity of data sharing, so you need to use IPC The data is separate, the synchronization is simple because of the sharing process data, the data sharing is simple, the synchronization complexity has its own advantages, CPU, memory occupies more memory, switching is complex, CPU utilization is low, memory occupies less memory, switching is simple, CPU utilization is high, multiple threads are better to destroy, switch creation and destruction, switching is complex, slow creation and destruction, simple switching. Speed multithreading is better coding coding is simple, debugging is convenient, debugging complex coding, debugging complex reliability processes run independently, do not affect each other thread breathing and common destiny multi-process better distributed can be used for multi-machine and multi-core distributed, easy to expand can only be used for multi-core distributed multi-process
Use multithreading to solve the calculation problem of the above code:
/ / api.jsconst Koa = require ('koa'); const app = new Koa (); const {Worker} = require (' worker_threads') app.use (async (ctx) = > {const url = ctx.request.url If (url ='/') {ctx.body = {name: 'xxx', age: 14}} if (url =' / compute') {const sum = await new Promise (resolve = > {const worker = new Worker (_ _ dirname+'/compute.js') / / receive message worker.on ('message') Data = > {resolve (data)}) ctx.body = {sum}) app.listen (4000, () = > {console.log ('http://localhost:4000/ start')}) / / computer.jsconst {parentPort} = require (' worker_threads') let sum=0for (let I = 0 I {const url = ctx.request.url If (url = ='/') {ctx.body = {name: 'xxx', age: 14}} if (url =' / compute') {const sum = await new Promise (resolve = > {const worker = fork (_ _ dirname+'/compute.js') worker.on ('message') Data = > {resolve (data)}) ctx.body = {sum}}) app.listen (4000, () = > {console.log ('http://localhost:4000/ start')}) / / computer.jslet sum=0for (let I = 0 I
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.