DEV Community

Ruheni Alex
Ruheni Alex

Posted on • Edited on • Originally published at ruheni.dev

2

Child Processes👶

Child Processes

For this article, it will assume you have a basic understanding of NodeJs and intermediate programming knowledge in JavaScript. For you to be able to get a firm grasp of child processes, you will need have a fair understanding of streams and event emitters. The final assumption I am going to make is that you have a basic knowledge of terminal commands used to manipulate files and folders.

What exactly is a child process?

A child process is a process that is created by another process. Sounds meta, right…? It is important to remember that JavaScript is single threaded and therefore we would not want to block the main thread running, otherwise our application will be slow and it won’t be able to do much really. In case you are asking “what the heck is he talking about …?”😕 don’t panic yet.

JavaScript Event loop explains how exactly JavaScript runs when executed 😉.

With child process, you are able to run multiple processes seamlessly and improve performance of your application. You are able to control the input stream and listen to its output stream, and pass it to another process, the same way the pipe | operator works in Linux

Child processes enable you to access the operating system functionalities by running a child process in it. There are four different ways of creating child processes and they vary in terms of speed (some slightly more efficient than others) and functionality. You create a child process through the following ways: fork(), spawn(), exec(), and execFile().

For you to have access to this module, you will have to import it to your program and destructure it to access the specific child process you would like to use. The examples i am going to use in this article are fairly simple😁.

Since I would not want to make this tutorial too long, I will cover only spawn().

const { spawn, exec, execFile, fork } = require("child_process");
Enter fullscreen mode Exit fullscreen mode

Spawn

This launches a new command in a new process. With spawn, you can pass any argument to it to carry out a given process. spawn is able to handle events, that is, you can register events so as to carry out a given task when the signal is sent.

The following events can be registered when using spawn():

  1. error - is emitted when the process could not be spawned or killed.
  2. message - it is emitted when child uses process.send() to send a message. It enables communication between the child and parent processes.
  3. disconnect - when parent process manually calls child.disconnect()
  4. close - emitted when the input and output streams of the given process are closed.
  5. data - the data event is emitted when the given stream is readable and we would like to manipulate the input and output the data. It can also be emitted in case of an error.

For those who are unfamiliar with the pwd command, it used to show the current working directory.

const { spawn } = require("child_process");
const pwd = spawn("pwd");

pwd.stdout.on("data", data => console.log(`path: ${data}`));

pwd.stderr.on("data", data => {
  console.error(`child stderr: ${data}`);
});

pwd.on("exit", (code, signal) => {
  console.log(`child process exited with ${code} and signal ${signal}`);
});
Enter fullscreen mode Exit fullscreen mode

Output:

path: "/c/Users/Users/Documents/pandora's_box/sandbox/nodejs"
child process exited with 0 and signal null
Enter fullscreen mode Exit fullscreen mode

Optionally, the spawn function is able to take in a second parameter which is an array containing strings which are the parameters one would like to use when executing a given command.

const { spawn } = require("child_process");
const py = spawn("py", ["--version"]);

py.stdout.on("data", data => console.log(`python version: ${data}`));

py.stderr.on("data", data => {
  console.error(`child stderr: ${data}`);
});

py.on("exit", (code, signal) => {
  console.log(`child process exited with ${code} and signal ${signal}`);
});
Enter fullscreen mode Exit fullscreen mode

Output:

python version: Python 3.7.3
child process exited with 0 and signal null
Enter fullscreen mode Exit fullscreen mode

The output will vary with your current working directory and code 0 means there was no error encountered when executing the program...

Happy hacking 🎉😁

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay