Cpu intensive task using nodejs | Child Process
Created on: Jul 28, 2024
The fork
method of the child_process
module in Node.js is used to create a new Node.js process (a child process) that runs a specified script. This child process has its own independent execution context, but it shares the same V8 engine as the parent process. The fork
method is a special case of the spawn
method, designed specifically to create Node.js processes.
Syntax
const { fork } = require("child_process"); const child = fork(modulePath, [args], [options]);
Example Usage
// piEstimationChild.js const estimatePi = require("./montecarlo"); const numPoints = parseInt(process.argv[2], 10); const result = estimatePi(numPoints); console.log(result); process.send(result); process.exit(0);
// piEstimationTaskChildProcess.js const { fork } = require("child_process"); const numPoints = 1e10; const runEstimation = (numPoints) => { return new Promise((resolve, reject) => { const child = fork("./piEstimationChild.js", [numPoints]); child.on("message", (message) => { resolve(message); }); child.on("exit", (code) => { if (code !== 0) { reject(new Error(`Child process exited with code ${code}`)); } }); child.on("error", (error) => { reject(error); }); }); }; Promise.all([ runEstimation(numPoints), runEstimation(numPoints), runEstimation(numPoints), ]) .then((results) => { results.forEach((result, index) => { console.log(`Estimation ${index + 1}: Pi is approximately ${result}`); }); }) .catch((err) => { console.error("An error occurred:", err); });
Advantages of Using fork
- Parallel Processing: Allows you to run multiple Node.js processes in parallel, taking advantage of multi-core processors.
- Isolation: Each child process has its own memory and execution context, reducing the risk of conflicts and memory leaks.
- Easy Communication: The IPC channel simplifies communication between parent and child processes, making it easy to exchange messages.
Use Cases
- CPU-bound Tasks: Offloading CPU-intensive tasks to child processes to prevent blocking the main event loop.
- Background Processing: Running background tasks or services that need to operate independently of the main application.
- Scalability: Scaling applications by spawning multiple processes to handle different parts of the workload.
Key Differences
Feature | Child Processes | Worker Threads |
---|---|---|
Isolation | Complete isolation (separate memory) | Shared memory space |
Communication | IPC with message passing (serialization) | Message passing, SharedArrayBuffer |
Startup Time | Relatively slow | Faster |
Resource Usage | Higher memory usage | Lower memory usage |
Use Cases | Independent tasks, microservices | Low-latency tasks, shared memory needs |
Error Impact | Errors in child do not affect parent | Errors can affect the main thread |
Threading: Child process creates a completely new and separate process, not a thread while worker thread
module create new thread within same process.
You can check full code in my github repo