[Node.js] Child Process with fork() to handle heavy calculation process
2023-09-14 08:59:15 时间
When build server, if we have a API endpoint requires some heavy calculation process, it will block the whole world. In this post, we will see how to use 'child_process' fork() to solve the problem;
Let's see the blocking code example:
const http = require('http'); const longComputation = () => { let sum = 0; for (let i = 0; i < 1e9; i++) { sum += i; } return sum; } const server = http.createServer(); server.on('request', (req, res) => { if (req.url === '/compute') { const sum = longComputation(); return res.end(`Sum is ${sum}`); } else { res.end('Ok'); } })
When we request '/compute' API endpoint, because of 'longCompute', it blocks the world, no other request can go thought.
curl localhost:3000/compute
Let's see how to fix it:
1. We will create a compute.js to hold the 'longCompute' function and also listen to the 'message' event on the global:
// Compute.js const longComputation = () => { let sum = 0; for (let i = 0; i < 1e9; i++) { sum += i; } return sum; } // listen the mssage event on the global // then do the computation process.on('message', (msg) => { const sum = longComputation(); process.send(sum); })
2. In server.js, instead of calling 'longCompute' directly, we fork the compute.js;
Send signal to tell the compute file to start doing the job, then we also need to listen to the event come back after computation is done.
// start processing compute.send('start');
// listen to the message compute.on('message', sum => { res.end(`Sum is ${sum}`); });
Full code for server.js
const http = require('http'); const {fork} = require('child_process'); const server = http.createServer(); server.on('request', (req, res) => { if (req.url === '/compute') { const compute = fork('compute.js'); // start processing compute.send('start'); // listen to the message compute.on('message', sum => { res.end(`Sum is ${sum}`); }); } else { res.end('Ok'); } }); server.listen(3000)
相关文章
- Node.js中Process.nextTick()和setImmediate()的区别
- Node.js这么下去...
- [Node.js] Add node.js command line to global
- [Node.js] node-persist: localStorage on the server
- [Node.js] Intro to Web Scraping with Node and X-ray
- [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2
- [Node.js] Using npm link to use node modules that are "in progress"
- 使用PM2管理Node.js集群
- [Node.js] Add node.js command line to global
- [RxJS] Convert a Node.js style callback to Observable: bindNodeCallback
- [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
- [Node.js] Node Util Promisify - How to Convert Callback Based APIs to Promise-based
- [Node.js] Create a model to persist data in a Node.js LoopBack API
- [Whole Web, Nods.js, PM2] Passing environment variables to node.js using pm2
- 利用Node.js为Node.js生成HttpStatusCode辅助类并发布到npm
- Typora+PicGo+Gitee+ node.js四大神器 实现自动图片上传功能 妈妈再也不用担心我的图床的问题了 图解过程 容易到没有朋友
- [0] Node.js
- Node.js http模块