node.js子进程-生成和派生之间的区别

这似乎是一个基本问题,但是我找不到任何文档:

分叉和生成node.js进程有什么区别?我已经读过分叉是生成的一种特例,但是使用它们的不同用例/重用分别是什么?

斯丁2020/04/07 11:34:47
  • spawn - child_process.spawn使用给定的命令启动一个新进程。
  • fork - child_process.fork方法是spawn()创建子进程的特例

spawn()方法

child_process.spawn method launches a new process with a given command. It has the following signature −

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

Read more about options

The spawn() method returns streams (stdout &stderr) and it should be used when the process returns a volume amount of data. spawn() starts receiving the response as soon as the process starts executing.

The fork() Method

child_process.fork method is a special case of spawn() to create Node processes. It has the following signature −

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

The fork method returns an object with a built-in communication channel in addition to having all the methods in a normal ChildProcess instance.