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

node.js Node.js

斯丁

2020-04-07

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

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

第4087篇《node.js子进程-生成和派生之间的区别》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
斯丁 2020.04.07
  • 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.

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android