Home  >  Article  >  Web Front-end  >  A brief discussion on how Nodejs performs multi-threading processing

A brief discussion on how Nodejs performs multi-threading processing

青灯夜游
青灯夜游forward
2021-06-04 11:14:503292browse

This article will introduce to you how Nodejs performs multi-threading processing. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on how Nodejs performs multi-threading processing

Threads à gogo is a native module of nodejs. Using this module can enable nodejs to have multi-thread processing capabilities. [Recommended learning: "nodejs Tutorial"]

Installation method

npm install threads_a_gogo

Download test source code

git clone http://github.com/xk/node-threads-a-gogo.git  

Import module code

var tagg= require('threads_a_gogo');

API

tagg= require('threads_a_gogo') //生成tagg object
var thread = tagg.create( /* no arguments */ ) //生成 thread object
var thread_pool = tagg.createPool( numberOfThreads ) //生成 thread pool
thread.load("boot.js").eval("boot()").emit("go").on("event", cb) //thread读取boot.js文件 执行boot(),发送event go,并且监听 event 事件 cb(err,result)
thread_pool.load('path') //pool中的任意(.all 为全部)thread 读取path file
pool.any.eval( program, cb ) //pool中的任意(.all 为全部)thread执行program

Why use multi-threading?

1. Parallel execution, no need to queue, fast.

2. Fairness, all threads have the same priority.

3. Make full use of resources and allow more CPUs to participate in task processing.

4. All threads share a storage address.

Example

Let’s do a simple test first, use the Fibonacci array to look at it, and add a multi-threaded node How powerful it is: (The test machine is 4CPU) Without the normal use of TAGG, asynchronous cannot help us deal with cpu-intensive tasks

function fibo (n) {
return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}
var n=8
function back(){
if(!--n) return console.timeEnd('no thread');
}
console.time('no thread');

process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})

process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})
process.nextTick(function(){
console.log(fibo (40));
back();
})

We simulated 8 asynchronous behaviors for testing node v0.8.16 version, so process.nextTick is still an asynchronous method. Finally, our output result is:

165580141
165580141
165580141
165580141
165580141
165580141
165580141
165580141
no thread: 23346ms

Next, we use the TAGG module to test the same execution of 8 Fibonacci array calculations and see what the result is?

function fibo (n) {
return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}
console.time('8 thread');
var numThreads= 8; //创建线程池,最大数为8
var threadPool= require('threads_a_gogo').createPool(numThreads).all.eval(fibo); //为线程池注册程序
var i=8;
var cb = function(err,data){ //注册线程执行完毕的回调函数
console.log(data);
if(!--i){
threadPool.destroy();
console.timeEnd('8 thread');
}
}
threadPool.any.eval('fibo(40)', cb); //开始向线程池中执行fibo(40)这个任务
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);
 
threadPool.any.eval('fibo(40)', cb);

The most important result:

165580141
165580141
165580141
165580141
165580141
165580141
165580141
165580141
8 thread: 9510ms

Compared to the node that does not use the multi-threading model, after using the TAGG module, our test results on the 4CPU server are one step faster More than twice that.

The TAGG module has many other functions, such as event triggering, smooth exit, checking thread working status, etc. In short, the TAGG module injects new vitality into the node. node has been criticized for handling cpu-intensive tasks and has been properly solved. Even if you are not good at c code, you can easily write multi-threaded and truly non-blocking node program.  

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of A brief discussion on how Nodejs performs multi-threading processing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete