当前位置:网站首页>Nodejs enables multi process and inter process communication
Nodejs enables multi process and inter process communication
2022-07-24 06:50:00 【AC it's really fragrant】
JS Whether in browser or Node The environment is single threaded , But you can call API Open multiple processes to improve the utilization of hardware resources .
The following is an introduction to nodejs The way to start multiple processes and realize communication .
process vs Threads
process : Resource allocation Minimum unit of
Threads :CPU Dispatch Minimum unit of
Start sub process mode
- child_process.fork
- cluster.fork
communication mode
Use send and on The message
Usage scenarios in two ways
child_process.fork: Used to start a single calculation , Complete the work with large amount of calculation
cluster: Integrate , It is applicable to the scenario where multiple processes start multiple network services
Code
Mode one : child_process.fork
The main process process.js
const http = require('http')
const fork = require('child_process').fork
const server = http.createServer((req, res) => {
if (req.url == "/get-sum") {
console.log(' The main process id', process.pid);
// Start subprocess
const computedProcess = fork('./computed.js')
computedProcess.send(' Start calculating ')
computedProcess.on('message', data => {
console.log(' Information received by the main process ', data);
res.end('sum is ' + data)
})
computedProcess.on('close', () => {
console.log(' The subprocess exited due to an error ')
computedProcess.kill()
res.end('error')
})
}
})
server.listen(3000, () => {
console.log('http://localhost:3000');
})
Subprocesses computed.js
function getSum() {
let sum = 0
for (let i = 0; i < 10000; i++) {
sum += i
}
return sum
}
process.on('message', data => {
console.log(' Subprocesses id', process.pid);
console.log(' The information received by the child process ', data);
const sum = getSum()
// Send a message to the main process
process.send(sum)
})
Mode two :cluster.fork
const http = require('http')
const cpuCoreLength = require('os').cpus().length
const cluster = require('cluster')
if (cluster.isMaster) {
for (let i = 0; i < cpuCoreLength; i++) {
cluster.fork() // Start subprocess
}
cluster.on('exit', worker => {
console.log(' Subprocess exit ');
cluster.fork() // Process daemons
})
} else {
// Multiple child processes will share one TCP Connect , Provide a web service
const server = http.createServer((req, res) => {
res.writeHead(200)
res.end('done')
})
server.listen(3000)
}
// In the work PM2
边栏推荐
- 【学习笔记】Web页面渲染的流程
- You don't know these pits. You really don't dare to use BigDecimal
- JMeter distributed pressure measurement
- Special effects - mouse click, custom DOM follow move
- [audio decoding chip] Application of vs1503 audio decoding chip
- Kubernetes rapid installation
- Redis特殊数据类型-BitMap
- 神经网络超参数调整(基于ray包)
- kubernetes简介和架构及其原理
- 今天聊赖数据库MySQL底层架构设计,你了解多少?
猜你喜欢
![[lvgl layout] grid layout](/img/36/47f586f3dc1a114ed7775c4e190872.png)
[lvgl layout] grid layout

ESP32超详细学习记录:NTP同步时间

oss授权单个bucket权限

STM32基于 FatFs R0.14b&SD Card 的MP3音乐播放器(也算是FatFs的简单应用了吧)

MGR_mysqlsh_keepalive高可用架构部署文档

Neural network superparameter adjustment (based on ray package)

神经网络超参数调整(基于ray包)

metaRTC5.0实现君正的纯C的webrtc版IPC

【媒体控制器】开源项目学习笔记(基于Arduino Micro开发板)

【微信小程序】一文搞懂条件渲染、列表渲染以及wxss模板样式
随机推荐
kubernetes 的Deployment(部署),Service概念,动态扩缩容
String问题
Introduction, architecture and principle of kubernetes
【媒体控制器】开源项目学习笔记(基于Arduino Micro开发板)
Experiment: creation, expansion, and deletion of LVM logical volumes
nodejs开启多进程并实现进程间通信
[lvgl layout] flexible layout
(static, dynamic, file) three versions of address book
Take you to understand the inventory deduction principle of MySQL database
Special effects - starry cosmic background effects
Redis入门
xxl执行节点错误日志刷屏
Browser local storage
极客星球丨 字节跳动一站式数据治理解决方案及平台架构
PyQt5入门——学生管理系统
[lvgl (3)]
Why can't index be the key of V-for?
JSONObject按照key的A——Z顺序排序
反射
Redis special data type bitmap