December 2018
Intermediate to advanced
642 pages
15h 5m
English
Since the code of the previous section used .spawn() to launch a new Node instance and run some code, it's fairly obvious that we can quickly and simply adjust it to use .fork() instead. Also, we won't have to use stdin and stdout to communicate, opting for messaging instead.
First, let's start with the parent code. It would become the following; the key differences are the usage of .fork() instead of .spawn(), and the way that the file path is sent to the child process:
// Source file: src/process_fork.jsconst path = require("path");const { fork } = require("child_process");const child = fork(path.resolve("out/process_fork_dir.js"));child.send({ path: "/home/fkereki" });child.on("message", data => { console.log(String(data)); ...Read now
Unlock full access