Nodejs multi-process child_process

The NodeJS multiprocess introduces the child_process module

const child_process = require('child_process')
Copy the code

Exec, one of the multi-process methods

Exec executes the command, and its return value is an object, like an instance of the Event class, that can listen for exit events via ON, with a code embedded in the callback function to return an exit code.

When executing a command,child_process causes a child process to run string commands. Windows and Linux can run system-specific commands, such as ls. The advantage of doing this is that it does not block the very thing that JS is trying to do, but rather dispatches a task for another process to complete. The disadvantage is that creating and destroying processes and process communication wastes computing and memory resources. Nodejs program does not take up much memory, so it can increase the process appropriately to improve the execution efficiency of the program. For example, when I want to do a calculation task, I can use EXEC to make C language or other languages perform the task and get the return value instead of computing in the main process. Nodejs’ advantage is not computing. So it’s better to let NodeJS do what she’s better at doing with IO.

const workerProcess = child_process.exec('node index1.js '+1.function(err,stdout,stderr){
    
})
workerProcess.on('exit'.(code) = >{
    // The default exit code is 0
})
Copy the code

Exec takes the command String as the first argument and the callback Function as the second argument. The first argument is an error, the second is the command line output result, and the third is the command line output error.

The first error may be a node typo such as (nnde) that causes the command to fail. The third error may be a program error rather than a command line error.

The exec function returns a child process object, workerProcess, which listens for the child process to exit. I think one of its uses is to start distributing new child processes at exit time to prevent the number of processes from increasing indefinitely.

The second multi-process method is SPwan

The obvious difference between spwan and exec is that spwan arguments are wrapped in arrays, such as child_process.exec(‘node index.js’); And child_process. Spwan (‘ node ‘[‘ index. Js]).

In addition,spwan returns objects containing stream objects,stdout and stderr, which listen for the data stream. Stdout is like the second parameter of exec callback,stderr is like the third parameter, and they listen for the console output to call back the parameters of the function The form returns. WorkerProcess can listen for exit events just like exec, its listening key is close, and the callback function is the same as exec’s callback function’s argument, which returns code and defaults to 0.

const workerProcess = child_process.spwan('node'['index.js']);
workerProcess.stdout.on('data'.function(data){
    console.log('spawn cmd data :'+data);
})
workerProcess.stderr.on('data'.function(data){
    console.log('spawn cmd error data :'+data);
})
workerProcess.on('close'.function(code){
    console.log('spawn cmd close :'+code);
})
Copy the code

The third fork of a multiprocess method

The third method has a significant difference in parameter passing from the first two methods. The first two methods write the full command line command string. Fork does not use and it only executes nodejs files and does not have node in front of it. Pass arguments in a similar way to spawn, see the code below:

child_process.fork('index.js');
child_process.fork('index.js'[1.2.3]);//1,2, and 3 as arguments to the command, such as node index.js 1,2,3,spwan can also be used
Copy the code

Js 1,2, and 3. Spwan can also carry arguments to the end of the command, which can be obtained from process.argv.slice(2).

The purpose of writing this article is to deepen their memory of unfamiliar knowledge points, if it can help people is to kill multiple birds with one stone. I remember cluster can also use multi-process? I will summarize and add to that next time I see it