Skip to content

Commit 905514d

Browse files
committed
Wip
1 parent 3744e2a commit 905514d

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

child-processes/index.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,84 @@ title: Child Processes
55

66
The `child_process` module provides the ability to spawn child processes. Whilst single-threaded, non-blocking performance in Node.js is great for a single process, but we can use `child_process` to handle the increasing workload in our applications.
77

8-
Child processes are spawned in a manner that is similar, but not identical, to [popen(3)](http://man7.org/linux/man-pages/man3/popen.3.html) from Linux programming using the `child_process.spawn()` function.
8+
Using multiple processes allows a Node application to scale. Node is designed for building distributed applications with many nodes, hence it's name Node.
99

10+
Child processes are spawned in a manner that is similar, but not identical, to [popen(3)](http://man7.org/linux/man-pages/man3/popen.3.html) from Linux programming using the `child_process.spawn()` function. This returns
11+
a `child` object (which is a `ChildProcess` object - which implements the `EventEmitter` API (link to events))
1012

11-
https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
13+
## ChildProcess Events
1214

15+
The `child` processes communicates by emitting events to let the `parent` know what is going on.
1316

17+
| Event Name | Reason For Emitting |
18+
|--------------|-----------------------------------------------------------------------------|
19+
| `disconnect` | The parent process manually calls `child.disconnect` |
20+
| `error` | The process could not be spawned or killed. |
21+
| `exit` | The exit code for the child and the optional signalthat was used to terminate it. When null, imples the child process exited normally. |
22+
| `close` | The `stdio` streams of a child process get closed. |
23+
| `message` | This `child` process uses the `send` method to communicate with the parent. |
1424

15-
Using multiple processes is the best way to scale a Node application. Node.js is designed for building distributed applications with many nodes. This is why it’s named Node. Scalability is baked into the platform and it’s not something you start thinking about later in the lifetime of an application.
25+
## Spawn Example
1626

27+
```javascript
1728

29+
const { spawn } = require('child_process');
1830

31+
const child = spawn('ls');
1932

33+
child.on('exit', function (code, signal) {
34+
console.log(`child process exited with code ${code} and signal ${signal}`);
35+
});
36+
child.on('exmessageit', function (code, signal) {
37+
console.log(`child process messaged with code ${code} and signal ${signal}`);
38+
});
39+
40+
spawn( ).stdout.pipe(process.stdout)
2041

2142
```
43+
The returned process object will hold a property for each std type represented as a Stream: .stdin - WriteStream, .stout - ReadStream and finally .stderr - ReadStream. Accordingly, if we would like to run git log through a Node process and print it to the console we would do something like the following:
2244

45+
Every child process also gets the three standard stdio streams, which we can access using child.stdin, child.stdout, and child.stderr.
2346

24-
As you probably know, our typical OS has different processes running in the background. 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. As such, we can’t 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.
47+
`spawn`
2548

49+
The first parameter is the path for an executable JavaScript file that starts the process, and the second argument is the arguments that will be passed into the process.
2650

2751

28-
Why is this module called child_process and not just process? First of all, not to confuse with the main process instance global.process, and second, the child process is derived from the main process, which means that both can communicate - the main process will hold streams for the child process’s std types and they will both share an ipc channel (“Inter Process Communication” channel; more on that further this article).
52+
53+
54+
There are four different ways to create a child process in Node: spawn(), fork(), exec(), and execFile().
55+
56+
Note that examples I’ll be using in this article are all Linux-based. On Windows, you need to switch the commands I use with their Windows alternatives. - find what they are...
57+
58+
59+
spawn - spwans a new process, which can pass the command any arguments
60+
2961

3062

3163

64+
So we can interact with the `ChildProcess` by listening for a series of events.
3265

3366

3467
```
3568
3669
37-
`spawn`
70+
As you probably know, our typical OS has different processes running in the background. 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. As such, we can’t 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.
3871
3972
40-
The first parameter is the path for an executable file that starts the process, and the second argument is the arguments that will be passed into the process.
4173
74+
Why is this module called child_process and not just process? First of all, not to confuse with the main process instance global.process, and second, the child process is derived from the main process, which means that both can communicate - the main process will hold streams for the child process’s std types and they will both share an ipc channel (“Inter Process Communication” channel; more on that further this article).
4275
4376
4477
4578
4679
80+
```
81+
82+
83+
4784

4885
More reading: [Node.js Child Processes: Everything you need to know
49-
](https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/)
86+
](https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/)
87+
88+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { spawn } = require('child_process');
2+
3+
const child = spawn('pwd');
4+
5+
child.on('exit', function (code, signal) {
6+
console.log(`child process exited with code ${code} and signal ${signal}`);
7+
});
8+
9+
child.on('message', function (message) {
10+
console.log(`child process messaged with message ${message}`);
11+
});
12+
13+
spawn('git', ['log']).stdout.pipe(process.stdout)
14+
spawn('ls').stdout.pipe(process.stdout)
15+

0 commit comments

Comments
 (0)