The child_process
module provides a new class called ChildProcess
that acts as a representation of the child processes that can be accessed from the parent. This allows you to control, end, and send messages to the child processes from the parent process that started them.
Also, the process
module is a ChildProcess
object. This means that when you access process
from the parent module, it is the parent ChildProcess
object, but when you access process
from the child process, it is the ChildProcess
object.
In this section you will learn more about the ChildProcess
object so that you will be able to leverage it in subsequent sections to implement multiprocess Node.js applications.
Table 9.4 lists the events that can be emitted on the ChildProcess
object. You implement handlers for the events to handle when the child process terminates or sends messages back to the parent.
Table 9.5 lists the methods that can be called on a child process. These methods allow you to terminate, disconnect, or send messages to the child process. For example, you can call the following code from the parent process to send an object to the child process:
child.send({cmd: 'command data'});
Table 9.6 lists the properties that you can access on ChildProcess
objects.