Transcript from the "Child Processes Solution" Lesson
>> Kyle Simpson: All right, let's talk about how we solve the final part of this exercise. I'm gonna rename that to sending so that we're clear what we're doing, we're sending these requests. All right, so what we wanna do, I think, is set up an array of children that is going to be filled by this for loop.
[00:00:19] So I'm gonna set (let i =0; i < MAX_CHILDREN; i++). And inside of this loop, what I'm going to do, is simply push the spawned object directly into that array.
>> Kyle Simpson: So basically I'm gonna end up at the end of that for loop with five child processes spawned.
[00:00:51] Now after that, I need to basically turn those five or however many child processes into a list of promises that will resolve after those child processes finish, whether they're successful or not. So, I'm gonna turn those children into an array of responses. I'm going to use .map for that.
>> Kyle Simpson: So the child, each child I'm gonna listen for the exit event, but remember I wanna turn these child objects into promises. So I need to return a new promise.
>> Kyle Simpson: And I need to determine if code is equal to 0, then I'm gonna resolve the promise with Boolean true.
[00:02:03] Otherwise, I'm gonna resolve the promise with Boolean false. So that lets me know whether or not I'm gonna have eventually an array of trues and falses, basically.
>> Kyle Simpson: Now I need to wait for all of those promises to resolve in whatever order they finish in before moving on.
[00:02:26] So I'm gonna simply say, Promise.all with those responses. So now resps is going to be an array of trues and/or falses in it. And I need to figure out if this array has all trues in it, then it was a success otherwise, it was a failure. And there's a little trick which is, if I filter the resps array with a function that filters out Booleans, like the Boolean function.
[00:03:01] If the length of that array is equal to MAX_CHILDREN, then all of them are true. Otherwise, if even one of them was removed, then the length won't be equal to true.
>> Kyle Simpson: I didn't uncomment it. Okay, there we go.
>> Kyle Simpson: All right, so we are now hammering our server with these child requests.
[00:03:50] Seems to be handling five concurrent requests pretty well. I say we amped the number of, what do you think?
>> Class: [INAUDIBLE]
>> Kyle Simpson: Let's go in increments. We'll see, I don't want us literally set my computer on fire. Let's try 100. If it handled five well, it's gonna take longer, but let's see how it does with 100.
>> Kyle Simpson: Taking a little bit longer. Remember, these server responses are waiting a second. There's an artificial delay in there so.
>> Kyle Simpson: It seems to be doing all right with 100 request. It's going a little slower. So, maybe we should up it to like 250. See what that does.
[00:04:42] Certainly gonna take longer.
>> Kyle Simpson: Remember, this is 250 child processes being spun up, all hammering the server and then all being shut down. And then 250 more spun up, hammering the server and then being shut down. So it's definitely taking a little bit longer this time but doing okay, no error so far.
>> Kyle Simpson: Let's try 500.
>> Student: I think I just heard the machine.
>> Kyle Simpson: I think I just heard my fan spin on. [LAUGH] I think we definitely crossed the limit where it's like, I gotta turn the fan on to start cooling this down. It's taking a while, but it hasn't failed, so maybe that's a good sign.
[00:05:47] We were able to do 500. All right, let's try 1,500, as was suggested by our good friend [LAUGH].
>> Student: Sparks will fly.
>> Kyle Simpson: Sparks may fly
>> Student2: Now, were you to decrease the delay timeout, would that in fact make it more difficult for the server because it would be getting a little more frequently or-
>> Kyle Simpson: Probably more frequently, yeah.
>> Student2: Okay.
>> Kyle Simpson: Cuz it's got some down time in here where it's able to recover.
>> Student2: I'm not suggesting that.
>> Kyle Simpson: I'm surprised it hasn't died yet. I thought it might have died by now but we'll see. We'll see if it's able to manage 1500 child processes at the same time.
>> Kyle Simpson: The suspense is killing me.
>> Student3: Says I see smoke.
>> Class: [LAUGH]
>> Kyle Simpson: I definitely hear fans.
>> Student3: Maybe that camera can pick up smoke.
>> Kyle Simpson: [LAUGH] There we go, it finally died. I wonder how many it was able to do, that was quite a few. Probably got almost over 1000.
[00:06:55] All right, so my little old machine is unable to handle that many requests, but.