You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: child-processes/index.md
+47-8Lines changed: 47 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,45 +5,84 @@ title: Child Processes
5
5
6
6
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.
7
7
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.
9
9
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))
|`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. |
14
24
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
16
26
27
+
```javascript
17
28
29
+
const { spawn } =require('child_process');
18
30
31
+
constchild=spawn('ls');
19
32
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)
20
41
21
42
```
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:
22
44
45
+
Every child process also gets the three standard stdio streams, which we can access using child.stdin, child.stdout, and child.stderr.
23
46
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`
25
48
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.
26
50
27
51
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
+
29
61
30
62
31
63
64
+
So we can interact with the `ChildProcess` by listening for a series of events.
32
65
33
66
34
67
```
35
68
36
69
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.
38
71
39
72
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.
41
73
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).
42
75
43
76
44
77
45
78
46
79
80
+
```
81
+
82
+
83
+
47
84
48
85
More reading: [Node.js Child Processes: Everything you need to know
0 commit comments