child_process
child_process is used to create spawned child processes. Node and spawned child processes establish stdin (standard input), stdout (standard output), stderr (standard error) pipes. child_process.spawn, child_process.fork, child_process.exec, child_process.execFile will all return ChildProcess instances. The ChildProcess instance implements the EventEmitter API, and event callback functions can be added to the child process instance. Processes can communicate with each other through the event message system.
child_process.spawn
Start a child process and execute the command. Spawn interface definition: spawn(command: string, args: ReadonlyArray<string>, options: SpawnOptions): ChildProcess
- command, the command to be run
- args, the parameters of the running command, is an array of strings
options, configuration items
- options.cwd working directory of the child process
- options.env environment variables, use key, value to configure environment variables
- and many more
- Return value ChildProcess, return an instance of ChildProcess
// 例子
const { spawn, spawnSync } = require('child_process')
const path = require('path')
const cp = spawn('ls', ['-a'], {
cwd: path.resolve(__dirname, '../Movies')
})
cp.stdout.on('data', (data) => {
console.log(`子进程输出:' ${data}`)
})
cp.on('exit', (code, signal) => {
console.log('子进程退出:', `code ${code} and signal ${signal}`)
})
Output result:
By default, the standard input, standard output, and standard error of the child process are redirected to the corresponding subprocess.stdin, subprocess.stdout and subprocess.stderr streams on the ChildProcess object. You can set options.stdio: inherit, pass in the parent process,
const { spawn, spawnSync } = require('child_process')
const path = require('path')
const cp = spawn('ls', ['-a'], {
cwd: path.resolve(__dirname, '../Movies'),
stdio: 'inherit'
})
child_process.spawnSync
The synchronous version of child_process.spawn. child_process.spawnSync returns Object. Object contains: pid (child process pid), stdout (standard output), stderr (standard error) and so on. The difference is that the function does not return until the child process is completely closed.
const { spawnSync } = require('child_process')
const path = require('path')
const obj = spawnSync('ls', ['-a'],{ cwd: path.resolve(__dirname, '../Movies') })
console.log('pid', `${obj.pid}`)
console.log('stdout', `${obj.stdout}`)
child_process.exec
Create a spawned shell, and then you can execute commands in the spawned shell. exec implements function overloading. The second parameter can be a configuration item or a callback. If the second parameter is a configuration item, then the third parameter is callback.
const { exec } = require('child_process')
const path = require('path')
exec('ls', (error, stdout, stderr) => {
if (error) {
console.error('error:', error);
return;
}
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
})
// 第二个参数可以是配置项
exec('ls -a', { cwd: path.resolve(__dirname, '../Movies'), }, (error, stdout, stderr) => {
if (error) {
console.error('error:', error);
return;
}
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
})
The three parameters of callback are error instance (if the execution is successful, error equals null), stdout standard output, stderr standard error.
child_process.execSync
The synchronous version of child_process.exec. The child_process.execSync method returns standard output, the difference is that the method does not return until the child process is completely closed.
const { execSync } = require('child_process')
const path = require('path')
const stdout = execSync('ls -a', { cwd: path.resolve(__dirname, '../Movies') })
console.log('stdout:', `${stdout}`)
The difference between child_process.exec and child_process.spawn
spawn
- Will not create a spawned shell
- Streaming data generated by the child process
- No data size limit
exec
- Will create a spawned shell
- Maximum transmission of 200kb of data
- Will cache data, transfer data after the process is closed
spawn is suitable for transmitting data for a huge long time. exec is suitable for situations that require multiple times and a small amount.
child_process.fork
child_process.fork, used to run the module in the child process. child_process.fork(modulePath [, args] [, options])
- modulePath, the address of the module that needs to be run in the child process
- args, string parameter list
options configuration item
- execPath, used to create the executable file of the child process. We can create child processes by specifying different versions of node by configuring this parameter.
- execArgv, a list of string parameters passed to the executable file.
- silent, the standard output of the child process, whether to inherit from the parent process. The default is false for inheritance. If set to true, directly pipe to child.stdin, child.stdout, etc. of the child process.
- stdio, used to configure the pipeline established between the parent process and the child process
// 子进程的代码
console.log('我是子进程')
const { fork } = require('child_process')
const { resolve } = require('path')
// 我是子进程
fork(resolve(__dirname, './cp.js'), {
silent: false
})
// 没有打印
fork(resolve(__dirname, './cp.js'), {
silent: true
})
const { fork } = require('child_process')
const { resolve } = require('path')
const cp = fork(resolve(__dirname, './cp.js'), {
silent: true
})
cp.stdout.on('data', function (data) {
// stdout 中输出: 我是子进程
console.log('stdout 中输出:', `${data}`)
})
Through the stdout attribute, you can get the content output by the child process
child_process.execFile
child_process.execFile does not create spawned shells. The efficiency is higher than exec. child_process.execFile(file[, args] [, options] [, callback])
- file, can be the name or path of the executable file.
const { execFile } = require('child_process')
const { resolve } = require('path')
execFile('node', [resolve(__dirname, './cp.js')], (err, stdout, stderr) => {
if (err) {
throw err
}
console.log(`stdout: ${stdout}`)
})
The difference between child_process.exec and child_process.execFile
Internally, exec is implemented by calling execFile. Internally, execFile is implemented by calling spawn.
event
Many events can be monitored on the ChildProcess instance
- close, triggered when the stdio stream of the child process is closed
- disconnect, triggered when the parent process manually calls the child.disconnect function
- error, will trigger when an error occurs
- exit, triggered when the child process exits
- message, triggered when the child process uses the process.send function to deliver a message
const { fork } = require('child_process');
const cp = fork('./cp.js')
cp.on('close', (code, signal) => {
console.log('close 事件:', code, signal);
})
cp.on('disconnect', () => {
console.log('disconnect 事件...');
})
cp.on('error', (code, signal) => {
console.log('error 事件:', code, signal);
})
cp.on('exit', (code, signal) => {
console.log('exit 事件:', code, signal);
})
cp.on('message', (val) => {
console.log('message 事件:', val);
})
Communication between processes
After the child process is created, an IPC channel will be created between the parent process and the child process, and the parent and child processes communicate through message and send.
// 父进程
const { fork } = require('child_process');
const cp = fork('./cp.js')
cp.on('message', (msg) => {
console.log('message: ' + JSON.stringify(msg))
})
// 子进程
process.send({
msg: '我是子进程'
})
The parent process can also send messages to the child process using cp.send
// 父进程
const { fork } = require('child_process');
const cp = fork('./cp.js')
cp.send({
msg: '我是父进程'
})
refer to
- Node.js v16.8.0 Document
- Node.js Child Processes: Everything you need to know
- exec vs execFile nodeJs
- Node.js Spawn vs. Execute
- Nodejs advanced: how to play the rotor process (child_process)
- depth understanding of processes and threads in Node.js
- Node.js process module learning guide
- play with node child process — child_process
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。