- In the last lesson, we used the execute function
to execute external system commands.
In this lesson, we're going to take a look
at the spawn function.
Now execute is designed to handle synchronous processes.
These are processes that run and close
and maybe spit back some data as
a one time event.
However execute is not meant to handle
a synchronous processes.
This could mean long running processes
or processes that are waiting for input
or anything that remains open.
Consider the app that we built back
in chapter four.
The question app.
I have it here within this start folder.
So if I type node questions we are asked three questions.
What is your name?
Alex.
Where do you live?
Tahoe.
And notice this application is running.
It's asynchronous.
It's waiting for me to add some input
before it continues.
And once I answer the three questions,
those questions are displayed back to us
in the terminal.
So the process collects answers
from the terminal and it remains open
while it waits for answers.
So we wouldn't want to execute this process.
We would want to spawn it.
Let's go ahead and create a new file
called spawn-dot-J-S.
And from our spawn file the first thing
we're going to do is include our child process
by requiring it.
Now go ahead and get rid of this terminal
for a little bit.
And what I'm going to do is I'm going to create
a question app variable and we're going to
run this question app from this process.
So we'll use C-P-spawn.
Now spawn wants to know the command
that we're going to spawn so that's
the first thing that we would type.
And then all of the additional arguments
have to come in as an array.
So the next thing that we would type
is questions-dot-J-S.
So now we'll go ahead and run our questions module
with Node.js in a separate process.
So we can now communicate with this process
through standard input and standard output.
For instance I can say
question app-dot-S-T-D-out-dot-on-data.
So we're going to use the standard output object
of our question app.
This means that when our question app spits
any data back out to the terminal,
we can handle it with this callback function.
So we'll go ahead and say console-dot-log
from the question app and we'll go ahead
and take a look at the data.
So when the question app spits out any data
we'll go ahead and log it to the console.
So the other thing we can do is listen
for when the question app closes.
So when there's a close event,
what we're going to go ahead and do
is console log...
Question app process exited.
Great.
So let's go ahead and open up our terminal.
And I'll go ahead and make it
a little more smaller just so
we can see the code too.
And then we will go ahead and node
our spawn module.
And we see we have an error.
Cannot find module CP.
That's because it's not called CP
it's called child underscore process.
CP is what I called the instance
so that was a mistake that I made.
Let's go ahead and try this again.
Come down here and type clear
and we'll type node spawn.
And in doing so we'll notice that
we have spawned the question app
and the question app is asking
the first question.
Notice that it hasn't closed.
That's because this is an asynchronous app.
So it's going to remain open until we provide
the three answers for the questions.
I'm going to go ahead and close this
by typing control + C and then I'll go ahead
and kill this terminal.
So we can send data to our spawn process
using standard input.
I can answer all three of the questions at once
by using question app-dot-S-T-D-I-N
and the standard input is a writeable stream
so I can write the data.
So I'll answer the first question with Alex
and I'll just go ahead and copy this line
so we are going to send Alex as
the first set of data.
Tahoe as the second set of data
to answer the second question.
And change the world as the third answer.
So as you can see we're spawning a process
and we're communicating with that process
through standard input and standard output.
So let me go ahead and run our application.
And when we do so you'll notice
that the application runs all at once
because we're answering all three questions
at once on lines five, six, and seven.
So it says what is your name?
Where do you live?
It's printing all of this information
to the terminal because we're listening
for data from our question app
and then printing that to the terminal.
Saying from the question app.
And then there are our answers.
So the question app has gathered
our Alex, tahoe, and change the world.
We also see that because we've answered
all three questions, the process
has exited.
So listening to that close event
has caused line 14 to be triggered
and we see that the question app process has exited.