Home >Web Front-end >JS Tutorial >An article to talk about node's multi-processing and multi-threading
This article will take you to understand node.js, introduce multi-process and multi-threading in node, and compare multi-process and multi-thread. I hope it will be helpful to everyone!
In node.js, the execution of javascript code is single-threaded Execution, but Node itself is actually multi-threaded.
The node itself is divided into three layers
The first layer, Node .js standard library, this part is written in Javascript, that is, the API that we can call directly during use, which can be seen in the lib directory in the source code.
The second layer, Node bindings, this layer is the key for Javascript to communicate with the underlying C/C. The former calls the latter through bindings and exchanges data with each other. It is the first layer and Third level bridge.
The third layer is the key to supporting the operation of Node.js. It is implemented by C/C and is some of the underlying logic implemented by node.
Among them, the third layer of Libuv provides Node.js with cross-platform, thread pool, event pool, asynchronous I/O and other capabilities, which is the key to making Node.js so powerful.
Because Libuv provides an event loop mechanism, JavaScript will not block in terms of io processing. Therefore, when we use node to build web services, we do not need to worry about excessive io volume causing other requests to be blocked.
However, the execution of non-io tasks is executed in the node main thread, which is a single-thread execution task. If there are very time-consuming synchronous computing tasks, it will block the execution of other codes.
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 <100000000000 ; i++) { sum+=i } ctx.body={sum} } }) app.listen(4000, () => { console.log('http://localhost:4000/ start') })
In the above code, if http requests /compute
, node will call the cpu to perform a large number of calculations. At this time, if other http requests come in, blocking will occur. .
So how to solve this problem?
There are two solutions, one is to use children_process
or cluster
to start multiple processes for calculation, and the other is to use worker_thread
Turn on multi-threading for calculation
Multi-process vs multi-thread
Compare multi-threading and multi-process:
Attributes | Multiple processes | Multiple threads | Compare |
---|---|---|---|
Data | Data sharing is complex and requires IPC; data is separated and synchronization is simple | Because process data is shared, data sharing is simple and synchronization is complex | Each has its own merits |
CPU, memory | Occupies a lot of memory, complex switching, low CPU utilization | Occupies little memory, simple switching, high CPU utilization | Multi-threading is better |
Destruction and switching | Creation, destruction, and switching are complex and slow | Creation, destruction, and switching are simple and fast | Multi-threading is better |
coding | Simple coding, convenient debugging | Coding, complex debugging | Coding, Complex debugging |
Reliability | Processes run independently and will not affect each other | Threads share the same fate | Multiple processes more Good |
Distributed | Can be used for multi-machine multi-core distribution, easy to expand | Can only be used for multi-core distribution | Multi-process is better |
Use multi-threading to solve the calculation problem of the above code:
//api.js const 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') //接收信息 worker.on('message', data => { resolve(data) }) }) ctx.body = {sum} } }) app.listen(4000, () => { console.log('http://localhost:4000/ start') }) //computer.js const {parentPort}=require('worker_threads') let sum=0 for (let i = 0; i <1000000000 ; i++) { sum+=i } //发送信息 parentPort.postMessage(sum)
Here is the official document,worker_threads
https://nodejs.org/dist/latest-v16.x/docs/api/worker_threads.html
Use multiple processes to solve the calculation problem of the above code :
//api.js const Koa = require('koa'); const app = new Koa(); const {fork} = require('child_process') 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 =fork(__dirname+'/compute.js') worker.on('message', data => { resolve(data) }) }) ctx.body = {sum} } }) app.listen(4000, () => { console.log('http://localhost:4000/ start') }) //computer.js let sum=0 for (let i = 0; i <1000000000 ; i++) { sum+=i } process.send(sum)
Here is the official documentation, child_process
https://nodejs.org/dist/latest-v16.x/docs/api/child_process .html
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of An article to talk about node's multi-processing and multi-threading. For more information, please follow other related articles on the PHP Chinese website!