|
1 | 1 | ---
|
2 |
| -layout: default |
| 2 | +layout: post |
3 | 3 | title: Child Processes
|
| 4 | +url: child-processes |
| 5 | +author: ian |
4 | 6 | ---
|
5 | 7 |
|
6 |
| -Coming soon. |
| 8 | +_Prerequisites: [events](../events), [streams](../buffers-and-streams)_ |
7 | 9 |
|
8 |
| -In the meantime, please see https://nodejs.org/api/child_process.html |
| 10 | +The typical operating system has different processes running in the background, and each process is being managed by a single-core of our CPU and will run a series of calculations each time it is being ticked. To take full advantage of our CPU using a single process, we would need a number of processes that is at least equal to the number of cores in our CPU. In addition, each process might be responsible for running a series of calculations of different logic, which will give the end user a better control over the CPU’s behavior. |
| 11 | + |
| 12 | +The `child_process` module provides the ability to spawn child processes and interact with other programs. Whilst single-threaded, non-blocking performance in Node.js is great for a single process, we can use `child_process` to handle the increasing workload in our applications in multiple threads. |
| 13 | + |
| 14 | +Using multiple processes allows a Node application to scale. Node is designed for building distributed applications with many nodes, hence it's name Node. |
| 15 | + |
| 16 | +Pipes for `stdin`, `stdout`, and `stderr` are established between the parent Node process and the spawned subprocess. The behaviour matches that of pipes in the shell. |
| 17 | + |
| 18 | +The examples in this article are all Linux-based. On Windows, you will need to switch the commands I use with their Windows alternatives or use a Linux-like shell. |
| 19 | + |
| 20 | +# `exec` vs `spawn` |
| 21 | + |
| 22 | +There are two main approaches to running child processesL `exec` and `spawn`. |
| 23 | + |
| 24 | +| `exec` | `spawn` | |
| 25 | +|------------------------------------------------------------|-------------------------------------------------------------| |
| 26 | +| spawns a shell in which the command is executed | Does not spawn a shell | |
| 27 | +| buffers the data (waits until the process finishes and transfers all the data ) | Streams the data returned by the child process (data flow is constant) | |
| 28 | +| maximum data transfer 200kb (by default | has no data transfer size limit | |
| 29 | + |
| 30 | +Typically, `spawn` is more suitable for long-running process with large outputs. spawn streams input/output with child process. `exec` buffered output in a small (by default 200K) buffer. |
| 31 | + |
| 32 | +## A Simple `exec` |
| 33 | + |
| 34 | +The `exec` method allows us to spawns a shell to execute another process, such as an executable script or another Node script. In this example, we will call the `ls` command to list files in the current directoy, with the optional param `-l` to show a long list of details. |
| 35 | + |
| 36 | +<div class="repl-code"> |
| 37 | + |
| 38 | +```javascript |
| 39 | + |
| 40 | +const { exec } = require('child_process'); |
| 41 | + |
| 42 | +exec('ls -l', (err, stdout, stderr) => { |
| 43 | + console.log(stdout); |
| 44 | +}); |
| 45 | + |
| 46 | +``` |
| 47 | +</div> |
| 48 | + |
| 49 | +For more details of the options that can be passed to `exec`, please see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback |
| 50 | + |
| 51 | +# spawn |
| 52 | + |
| 53 | +The `spawn` method works by returning a new process. The process object that is returned has properties for each standard IO represented as a `Stream`: `.stdin` (`WriteStream`), `.stout` (`ReadStream`) and `.stderr` (`ReadStream`). |
| 54 | + |
| 55 | +We will now implement the previous example using the `spawn` method: |
| 56 | + |
| 57 | +<div class="repl-code"> |
| 58 | + |
| 59 | +```javascript |
| 60 | + |
| 61 | +const { spawn } = require('child_process'); |
| 62 | + |
| 63 | +const ls = spawn('ls', ['-l']); |
| 64 | + |
| 65 | +ls.stdout.on('data', (data) => { |
| 66 | + console.log(`stdout: ${data}`); |
| 67 | +}); |
| 68 | + |
| 69 | +ls.stderr.on('data', (data) => { |
| 70 | + console.error(`stderr: ${data}`); |
| 71 | +}); |
| 72 | + |
| 73 | +ls.on('close', (code) => { |
| 74 | + console.log(`child process exited with code ${code}`); |
| 75 | +}); |
| 76 | + |
| 77 | +``` |
| 78 | +</div> |
| 79 | + |
| 80 | +In this example we are listening to the `stout` and `stderr` streams for `data` events, as well as listening for a `close` event. In contrast, the `exec` method uses a callback which buffers the streams to strings and an error object. |
| 81 | + |
| 82 | +# spawn using pipes |
| 83 | + |
| 84 | +We are going to run two child processes, and pipe the output from one as the input to another. We want to use `wc` (word count) to count the number of words in the output from the `ls` command we have been using so far. This is the same as running `ls -l | wc` on the Linux/Mac command line. |
| 85 | + |
| 86 | +<div class="repl-code"> |
| 87 | + |
| 88 | +```javascript |
| 89 | + |
| 90 | +const { spawn } = require('child_process'); |
| 91 | + |
| 92 | +const ls = spawn('ls', ['-l']); |
| 93 | +const wc = spawn('wc'); |
| 94 | + |
| 95 | +// pipe output from ls as input to wc |
| 96 | +ls.stdout.pipe(wc.stdin); |
| 97 | + |
| 98 | +wc.stdout.on('data', (data) => { |
| 99 | + console.log(`wc stdout: ${data}`); |
| 100 | +}); |
| 101 | + |
| 102 | +wc.stderr.on('data', (data) => { |
| 103 | + console.error(`wc stderr: ${data}`); |
| 104 | +}); |
| 105 | + |
| 106 | +wc.on('close', (code) => { |
| 107 | + console.log(`wc child process wc exited with code ${code}`); |
| 108 | +}); |
| 109 | + |
| 110 | +``` |
| 111 | +</div> |
| 112 | + |
| 113 | +# fork |
| 114 | + |
| 115 | +`fork` is a special version of `spawn` that allows messages to be sent between the Node processes. |
| 116 | + |
| 117 | +Unfortunately, we are unable to run this example in the REPL because it requires separate files. So this example is best run locally on your favourite terminal. |
| 118 | + |
| 119 | +In this example, we are going to create a child process that can receive a number, and then calculates the fibonacci of that number. This has been implemented inefficiently to illustrate long running processes. The `message` event is used to listen for requests, and the payload is destructed for a numerical value, `n`. |
| 120 | + |
| 121 | +```javascript |
| 122 | + |
| 123 | +const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2); |
| 124 | + |
| 125 | +process.on('message', ({ n }) => { |
| 126 | + process.send({ fib: fibonacci(n), n }); |
| 127 | + // optional - there is no reason why this child process |
| 128 | + // can't be called multiple times. |
| 129 | + process.exit(); |
| 130 | +}) |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +The parent process creates 3 child processes, and passes a range of numbers to them for calculating. |
| 135 | + |
| 136 | +```javascript |
| 137 | + |
| 138 | +const { fork } = require('child_process'); |
| 139 | + |
| 140 | +const child1 = fork('fork-child'); |
| 141 | +const child2 = fork('fork-child'); |
| 142 | +const child3 = fork('fork-child'); |
| 143 | + |
| 144 | +// send data to the child process to perform the calculation |
| 145 | +child1.send({ n: 5 }); |
| 146 | +child2.send({ n: 10 }); |
| 147 | +child3.send({ n: 45 }); |
| 148 | + |
| 149 | +child1.on('message', (m) => { |
| 150 | + console.log('PARENT child1 got message:', m); |
| 151 | +}); |
| 152 | +child2.on('message', (m) => { |
| 153 | + console.log('PARENT child2 got message:', m); |
| 154 | +}); |
| 155 | +child3.on('message', (m) => { |
| 156 | + console.log('PARENT child3 got message:', m); |
| 157 | +}); |
| 158 | + |
| 159 | +child1.on('exit', () => { |
| 160 | + console.log('child1 exited'); |
| 161 | +}); |
| 162 | +child2.on('exit', () => { |
| 163 | + console.log('child2 exited'); |
| 164 | +}); |
| 165 | +child3.on('exit', () => { |
| 166 | + console.log('child3 exited'); |
| 167 | +}); |
| 168 | +``` |
| 169 | + |
| 170 | +## ChildProcess Events |
| 171 | + |
| 172 | +The `child` processes communicates by emitting events to let the `parent` know what is going on. |
| 173 | + |
| 174 | +| Event Name | Reason For Emitting | |
| 175 | +|--------------|-----------------------------------------------------------------------------| |
| 176 | +| `disconnect` | The parent process manually calls `child.disconnect` | |
| 177 | +| `error` | The process could not be spawned or killed. | |
| 178 | +| `exit` | The exit code for the child and the optional signalthat was used to terminate it. When null, imples the child process exited normally. | |
| 179 | +| `close` | The `stdio` streams of a child process get closed. | |
| 180 | +| `message` | This `child` process uses the `send` method to communicate with the parent. | |
| 181 | + |
| 182 | +## A note on options |
| 183 | + |
| 184 | +These processes all accept an optional options object that allow us to control the context that the processes are run within. These vary for each method, and are described in detail in the [node documentation for `child_process`](https://nodejs.org/api/child_process.html). |
| 185 | + |
| 186 | +## Summary |
| 187 | + |
| 188 | +Parents `spawm`, `fork` or `exec` child processes, and communicate via events or pipes. |
| 189 | + |
| 190 | +Take me to [cheat sheet]({{ "/cheatsheet/#child-process" | url }}). |
| 191 | + |
| 192 | +## Exercise |
| 193 | + |
| 194 | +Create a child process for doing some manipulation of a file or URL, and build a parent process that controls a number of these processes in parallel. |
| 195 | + |
| 196 | +_No ideal solution as yet, but we would love to see a PR with one._ |
0 commit comments