...

/

Using External Processes (I)

Using External Processes (I)

Learn how to prevent the event loop from blocking using external processes.

Deferring the steps of an algorithm isn’t the only option we have for running CPU-bound tasks. Another pattern for preventing the event loop from blocking is using child processes. We already know that Node.js gives its best when running I/O-intensive applications such as web servers, which allows us to optimize resource utilization thanks to its asynchronous architecture. So, the best way we have to maintain the responsiveness of an application is to not run expensive CPU-bound tasks in the context of the main application and, instead, use separate processes. This has three main advantages:

  • The synchronous task can run at full speed, without the need to interleave the steps of its execution.
  • Working with processes in Node.js is simple, probably easier than modifying an algorithm to use the setImmediate() method, and allows us to easily use multiple processors without the need to scale the main application itself.
  • If we really need maximum performance, the external process could be created in lower-level languages, such as good old C or more modern compiled languages like Go or Rust. Always use the best tool for the job!

Node.js has an ample toolbelt of APIs for interacting with external processes. We can find all we need in the child_process module. Moreover, when the external process is just another Node.js program, connecting it to the main application is extremely easy and allows seamless communication with the local ...