当前位置:网站首页>Multi process model in egg -- egg document Porter
Multi process model in egg -- egg document Porter
2022-04-23 05:21:00 【process1212】
egg Multi process model in
2018 year 11 month 19 Japan
Multi process model
Master:
Under this model ,Master The process undertakes the work of process management ( similar pm2), Don't run any business code , We just need to run a Master Process, it will help us deal with all Worker、Agent The initialization and restart of the process .
Master The stability of the process is extremely high , When running online, we just need to pass egg-scripts Background operation passed egg.startCluster Starting up Master The process is OK , No need to use pm2 Wait for the process daemons .
Agent:
In most cases , When we write business code, we don't have to consider Agent The existence of processes , But when we come across some scenes , When you just want the code to run on a process ,Agent It's time for the process to work .
because Agent only one , And will be responsible for a lot of dirty work to maintain the connection , So it can't hang up and restart easily , therefore Agent The process will not exit when it listens for uncapped exceptions , But it will print out the error log , We need to be alert to uncapped exceptions in the log .
Worker:
Worker The process is responsible for handling real user requests and Timing task To deal with . and Egg The timed task also provides only one Worker The ability of a process to run , Therefore, the problems that can be solved through scheduled tasks should not be put into Agent On the implementation .
Worker Running business code , Relative ratio Agent and Master The code running on the process is more complex , The stability is also a little lower , When Worker When the process exits abnormally ,Master The process will restart a Worker process .
'use strict';
const cluster = require('cluster');
if (cluster.isMaster) {
const worker = cluster.fork();
worker.send('hi there');
worker.on('message', msg => {
console.log(`msg: ${
msg} from worker#${
worker.id}`);
});
} else if (cluster.isWorker) {
process.on('message', (msg) => {
process.send(msg);
});
}
app.messenger.broadcast(action, data): Send it to all agent / app process ( Including myself )app.messenger.sendToApp(action, data): Send it to all app process- stay app Calling this method will send it to yourself and others app process
- stay agent Calling this method will be sent to all app process
app.messenger.sendToAgent(action, data): Send to agent process- stay app Calling this method will send to agent process
- stay agent Calling this method will send to agent own
agent.messenger.sendRandom(action, data):- app There is no such method on ( Now? Egg The implementation of is equivalent to sentToAgent)
- agent Will send a random message to app process ( from master To control who to send )
app.messenger.sendTo(pid, action, data): Send to the specified process
// app.js
module.exports = app => {
// Be careful , Only in egg-ready The message cannot be sent until the event is received
app.messenger.once('egg-ready', () => {
app.messenger.sendToAgent('agent-event', {
foo: 'bar' });
app.messenger.sendToApp('app-event', {
foo: 'bar' });
});
}
egg-ready:
The above example mentioned , Need to wait egg-ready The message cannot be sent until the message is sent . Only in Master Make sure all the Agent The process and Worker All processes have started successfully ( and ready) after , Will pass messenger send out egg-ready News to all Agent and Worker, Tell me everything is ready ,IPC The channel is ready to use .
Accept :
stay message Listen for the corresponding action event , You can accept the information sent by other processes .
app.messenger.on(action, data => {
// process data
});
app.messenger.once(action, data => {
// process data
});
版权声明
本文为[process1212]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230519446107.html
边栏推荐
- Output string in reverse order
- 点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
- 我这位老程序员对时代危险和机遇的一点感悟?
- Data management of basic operation of mairadb database
- 2021-10-12
- Swing display time (click once to display once)
- How to set the initial value of El input number to null
- How to realize adaptive layout
- Top 25 Devops tools in 2021 (Part 2)
- Solve the problem of JS calculation accuracy
猜你喜欢

phphphphphphphp

How to set the initial value of El input number to null

5 minutes to understand MySQL row column conversion

Top 25 Devops tools in 2021 (Part 2)

Detailed explanation of concurrent topics

源码剖析Redis中如何使用跳表的

项目经理值得一试的思维方式:项目成功方程式

Servlet3 0 + event driven for high performance long polling

JSP-----JSP简介

Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
随机推荐
[untitled]
使用 Kears 实现ResNet-34 CNN
何时适合进行自动化测试?(下)
点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
Study notes: unity customsrp-12-hdr
【openh264】cmake: msopenh264-static
Master-slave replication of MariaDB database
Pandas to_ SQL function pit avoidance guide "with correct code to run"
Musk and twitter storm drama
Mariadb的半同步复制
学习笔记:Unity CustomSRP-12-HDR
Laravel implements the Holy Grail model with template inheritance
源码剖析Redis中如何使用跳表的
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
《2021年IT行业项目管理调查报告》重磅发布!
How to realize adaptive layout
The concept of meta universe is popular. Is virtual real estate worth investing
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
Good test data management, in the end how to do?
JS time format conversion