How to handle Child Threads in Node.js ? Last Updated : 10 Sep, 2021 Comments Improve Suggest changes Like Article Like Report Node.js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer.But node.js gives us ways to work around if we really need to do some work parallelly to our main single thread process.Child Process in Node: The child_process module gives the node the ability to run the child process by accessing operating system commands.Example: Filename: parallelProcess.js javascript // Do any work in parallel to main // event loop or main process console.log('Child Process Starts') setTimeout(() => { console.log('Data processed') }, 5000) Filename: main.js javascript const { fork } = require('child_process'); // Fork another process const child_process = fork('./parallelProcess.js'); // Data we may need to send to the child process const data = {} console.log('Before process') // Send the data to forked process child_process.send({ data }, function(){ console.log('Sending data') }); // Listen to forked process child_process.on('close', (result) => { console.log('Child process terminated and returned'); }); console.log('After process') Output: Before process After process Child Process Starts Data processed Child process terminated and returned Worker Threads The worker_threads module enables the use of threads that execute JavaScript in parallel. Worker's threads are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work which can be done better using Node.js built-in asynchronous I/O operations.Example: javascript const {Worker, isMainThread, parentPort} = require('worker_threads'); if (isMainThread) { // Main code, only executed for main thread const worker = new Worker(__filename, { workerData: {} }); worker.on('message', (m) => console.log( 'Thread send message:', m)); worker.on('error', () => console.log('Error')); worker.on('exit', () => { console.log('Worker exit') }); } else { // Worker thread code, only execute // for working thread. setTimeout(() => { parentPort.postMessage('Hello World!'); }, 3000) } Output: Thread send message: Hello World! Worker exit The process has its own memory space on other hand, threads use the shared memory space. Thread is part of the process. Since worker_threads make new threads inside the same process it requires fewer resources.Reference: https://nodejs.org/api/child_process.html https://nodejs.org/api/worker_threads.html Create Quiz Comment B bjindal Follow 0 Improve B bjindal Follow 0 Improve Article Tags : Web Technologies Node.js Node.js-Misc Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like