This project demonstrates the use of worker_threads module (released since Node.js v10.5.0).
In this project we will simulate a CPU intensive task to block the main thread and then show how to use worker threads in Node.js to run Javascript code in parallel.
- Create a directory
mkdir multi-thread
cd multi-thread
- Install express-generator globally, to create an application skeleton and then launch it.
npm i -g express-generator
express
npm install
- To perform image compression, install jimp (JavaScript Image Manipulation Program).
- A CPU intensive task without worker threads implementation is written in
/blockingAPI. - CPU intensive image compression run using worker_threads module is written in
/workersAPI. - Calling the
/blockingAPI will block the main thread and hence hinder the response time for rest of the APIs. - You will observer that the
/workersAPI does not block the main thread.