Check out a free preview of the full Digging Into Node.js course

The "Child Processes" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle sets up the functionality of creating child processes from a main process in Node, and demonstrates how to listen for the child process to exit using the object received from calling spawn to create the child.

Preview
Close

Transcript from the "Child Processes" Lesson

[00:00:00]
>> Kyle Simpson: Let's look at exercise seven. We're gonna shift gears back again, we were kind of moving our way up into the web stack for the last couple of exercises. We're gonna shift gears back to the command line, and we're gonna go real low level with this. We're gonna talk about about how to deal with Node across multiple process instances.

[00:00:18]
Specifically how to spin off child processes from a main node process and how to communicate with them, how to receive information back from them, that sort of thing. So, what we're gonna do is start by using the built-in Node childProc process. That's how we're gonna spin off a child process is using childProc.

[00:00:40]
For now, we're only gonna do one of these, but we're gonna end up spinning off multiples of these. Because the spirit of this exercise is now that we have a web server that's running, I thought maybe it'd be kind of fun to try to build a load tester that tries to spin up multiple child processes and hammer our web server.

[00:00:59]
And see how many child processes we can get before it crashes. So we're gonna be spinning up a load tester here in exercise seven. All right, so the basic concept behind a child process is that you end up spinning up this separate thing, and then you get back an object that has standard IO streams on it.

[00:01:21]
And that's how you know what input you might send to the child process and what output you might be getting back or errors you might be getting back from the child process. So we're gonna end up calling a method called spawn on this childProc module. That's gonna end up creating us an object that represents the child.

[00:01:45]
So let's try that.
>> Kyle Simpson: We'll call childProc.spawn. The executable is actually Node itself, we're gonna call Node again. And then the parameters that we wanna pass to Node are ex7-child.js. We'll talk about that one in just a moment. So we're gonna be spinning off a child process. Now child, as I said, that comes back, that has elements on it where we can listen to an event for it.

[00:02:35]
And we can also read and write from its standard IO. In our particular case, I think all we wanna do is ask, did it finish? So we're gonna basically say child.on("exit".
>> Kyle Simpson: And then we'll say console.log("Child finished") and then whatever that code was, okay? So we're gonna spin off this child process and then just simply wait until it finishes, and then we'll be notified that it finished.

[00:03:16]
>> Kyle Simpson: So what are we gonna put in our child process? Well for now, let's just do something simple so that we can test whether or not our child process communication stuff is working. Let's just do a long running for loop like (let i=0; i<1000000; i++), and then we'll say i = i.

[00:03:43]
We'll just let that for loop run. Or maybe we'll say,
>> Kyle Simpson: We'll let it run for half the length of that by double incrementing it, and then it'll be finished. So if we spin up this child process, it should just run for a brief, brief amount of time and then be completed.

[00:04:01]
So let's test that at the command line by just running our node ex7.js.
>> Kyle Simpson: And then if we run ex7-child.js, it just runs. It doesn't take very long, so maybe we should go back and make it take a little bit longer.
>> Kyle Simpson: Just for demonstration purposes, still pretty fast.

[00:04:27]
We might be running into V8 trying to optimize away this loop. So maybe I can get a little bit tricky and try to get it to not do that.
>> Kyle Simpson: There we go. Now I, at least, made it wait a little bit longer, so it waits about a second.

[00:04:54]
And then if we run ex7, then you notice it waits for a moment before telling us that the child has finished.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now