异步进程的创建


child_process.spawn()child_process.fork()child_process.exec()child_process.execFile() 方法都遵循其他 Node.js API 惯用的异步编程模式。

每个方法都会返回 ChildProcess 实例。 这些对象实现了 Node.js 的 EventEmitter API,使得父进程可以注册监听器函数(当在子进程的生命周期中发生特定事件时会被调用)。

child_process.exec()child_process.execFile() 方法还可以指定可选的 callback 函数(当子进程终止时会被调用)。

The child_process.spawn(), child_process.fork(), child_process.exec(), and child_process.execFile() methods all follow the idiomatic asynchronous programming pattern typical of other Node.js APIs.

Each of the methods returns a ChildProcess instance. These objects implement the Node.js EventEmitter API, allowing the parent process to register listener functions that are called when certain events occur during the life cycle of the child process.

The child_process.exec() and child_process.execFile() methods additionally allow for an optional callback function to be specified that is invoked when the child process terminates.