Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js几种创建子进程方法 #25

Open
CommanderXL opened this issue Jan 4, 2019 · 0 comments
Open

Node.js几种创建子进程方法 #25

CommanderXL opened this issue Jan 4, 2019 · 0 comments
Labels

Comments

@CommanderXL
Copy link
Owner

Node.js几种创建子进程方法

  • exec

其中exec可用于在指定的shell当中执行命令。不同参数间使用空格隔开,可用于复杂的命令。

const { exec } = require('child_process')
exec('cat *.js bad_file | wc -l')

exec方法用于异步创建一个新的子进程,可以接受一个callback

exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
  console.log(stdout)
})

传给回调的stdoutstderr参数会包含子进程的stdoutstderr的输出。

  • execFile
child_process.execFile(file[, args][, options][, callback])
const { execFile } = require('child_process')
execFile('node', ['--version'], (err, stdout, stderr) => {
  console.log(stdout)
})

不是直接衍生一个shell。而是指定的可执行的文件直接创建一个新的进程。

  • fork
child_process.fork(modulePath[, args][, options])

创建一个新的node子进程。调用该方法后返回一个子进程的对象。通过fork方法创建出来的子进程可以和父进程通过内置的ipc通道进行通讯。

衍生的 Node.js 子进程与两者之间建立的 IPC 通信信道的异常是独立于父进程的。 每个进程都有自己的内存,使用自己的 V8 实例。 由于需要额外的资源分配,因此不推荐衍生大量的 Node.js 进程。

其中在options的配置信息当中:

  • silent

父子进程间stdin/stdout/stderr之间的通讯。

如果置为true,那么子进程的标准输入输出都会被导流到父进程中:

parent.js
const { fork } = require('child_process')
const fd = fork('./sub.js')

fd.stdout.on('data', data => console.log(data))

sub.js
console.log('this is sub process')

一般在子进程中如果有stdin的时候,可将stdin直接导入到父进程中,这样可进行ttyshell的交互。

如果置为false,那么子进程的标准输入输出都会继承父进程的。

  • stdio

关于这个的配置见下文。

  • spawn
child_process.spawn(command[, args][, options])

上面说到的execexecFilefork创建新的子进程都是基于这个方法进行的封装。

调用这个方法返回子进程对象。

父子进程间的通讯

其中通过fork方法和spawn创建新的子进程时,在配置选项中有关于stdio的�字段:

这个字段主要用于父子进程间的管道配置。默认情况下,子进程的 stdinstdoutstderr 会重定向到 ChildProcess 对象上相应的 subprocess.stdinsubprocess.stdoutsubprocess.stderr 流。 这等同于将 options.stdio 设为 ['pipe', 'pipe', 'pipe']

  • pipe - 等同于 [pipe, pipe, pipe] (默认)
  • ignore - 等同于 [ignore, ignore, ignore]
  • inherit - 等同于 [process.stdin, process.stdout, process.stderr] 或 [0,1,2]

其中inherit即继承父进程的标准输入输出(和父进程共享)。

const { spawn } = require('child_process');

// 子进程使用父进程的 stdios
spawn('prg', [], { stdio: 'inherit' });

// 衍生的子进程只共享 stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

// 打开一个额外的 fd=4,用于与程序交互
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant