The node fork child process method.
I have been doing a lot of work revolving the use of the child process module as of late, so I thought I would write some demos about the node fork child process method. This node fork method does not launch a copy of the current process, but it does start a new node process with the given external script given as the first argument. It is similar to other methods in the child process module such as spawn, but it is set up a little different by default, and might be a better alternative to using spawn when it comes to launching an additional node process on the host os.
1 - node fork basic example
For just a basic example of the node fork child process method I made just a simple parent script that will launch one or more test scripts. This just seems like the thing to do when I first start working with one of these kinds of methods in nodejs.
So that being said I worked out a simple script that just uses the node fork method like this:
This will fire a basic-test.js file as a new process by default in the command line if not other script is given at the command line. For the moment the parent script does nothing other than just launching this child process via the node fork method. Now that I have this together lets look at some basic scripts to use with it.
1.1 - A basic node fork child script that just prints to the standard output
So one thing I noticed right off the bat compared to spawn is that when I write something to the standard output in the child process script that is launched with node fork, it shows up in the console. This differs from using the node spawn method where I have to set up a handler for the child process instance. So things are set up a little different by default when it comes to how the standard output is handled with the child process that is created with node fork.
So If I have a basic_test.js file like this
I can then use my main parent script to launch it.
1.2 - The standard input in a child script used with node fork
The same can be said of the standard input also, it is inherited from the parent script that used node fork to launch it.