This time I will show you how to use the cluster cluster in node, and what are the precautions for using the cluster cluster in node. The following is a practical case, let's take a look.
Conclusion
Although the number of worker processes is usually set to the number of CPU processes, this number can be exceeded, and the main process is not created first.if (cluster.isMaster) { // 循环 fork 任务 CPU i5-7300HQ 四核四进程 for (let i = 0; i In the main process, cluster represents the main process (used for monitoring and sending events), process is its own process, and worker represents the child process, which is obtained through cluster.workers<p style="text-align: left;"></p>In the child process process represents a child process (used for listening and sending events), or the current child process can be represented through cluster.worker<p style="text-align: left;"></p>cluster.worker.process is equivalent to process (in a child process)<p style="text-align: left;"></p> <p style="text-align: left;"><span style="color: #ff0000">Main process and child processes communicate with each other<strong></strong></span></p><p style="text-align: left;"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/7bf610320e74dbfb92ff934022d0032d-0.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><ol class=" list-paddingleft-2">##cluster is used to monitor process(child) child process triggers Various events<li><p style="max-width:90%"></p></li>#worker obtains in the main process and is used to communicate with itself. When the child process triggers an event, the current worker and related information will be returned to the corresponding event of the main process <li><p style="text-align: left;"></p></li>process(parent) The process instance of the main process itself, during the communication process The instance of the process(child) child process itself is basically not used. You can only obtain events for monitoring itself in the child process. <li><p style="text-align: left;"></p></li> It can be seen that the main process and the child process communicate with each other through such a triangular relationship, in which cluster and worker are obtained in the main process, and process(child) is the child process. The cluster notifies the child process by operating the worker, and the child process itself communicates with the cluster. Why is it designed this way? Because there will be multiple child processes, you can only choose which process to communicate with through the worker<li><p style="text-align: left;"></p></li>Scheduling policy for child processes cluster.schedulingPolicy</ol><p style="text-align: left;"></p><p style="text-align: left;">Scheduling Strategies, including cluster.SCHED_RR for cycle counting, and cluster.SCHED_NONE depending on the operating system. This is a global setting that will take effect immediately when the first worker process is spawned or cluster.setupMaster() is called. SCHED_RR is the default setting in all operating systems except Windows. Windows systems will also change to SCHED_RR as long as libuv can efficiently distribute IOCP handles without causing serious performance hits. cluster.schedulingPolicy can be implemented by setting the NODE_CLUSTER_SCHED_POLICY environment <span style="color: #ff0000"> variable <strong>. Valid values for this environment variable include "rr" and "none". </strong></span>RR is Round-Robin polling scheduling, that is, each child process has an equal chance of obtaining events, which is the default except for windows. The scheduling strategy under windows is very strange, as shown in the figure below. Currently, there is no relevant API to set the scheduling strategy algorithm. node only provides us with two values</p><p style="text-align: left;"><a href="http://www.php.cn/wiki/70.html" target="_blank"></a></p>Process scheduling algorithm.png<p style="text-align: left;"></p>The test data is 1000 concurrent requests, repeated test 20 times, performance under windows. It can be seen that the scheduling algorithm of windows is chaotic. If it is the RR algorithm, the scheduling of the four processes should be on the same horizontal line. We have not set up a local Linux environment for the time being. Students who have the conditions can help test it. <p style="text-align: left;">Cluster's scheduling algorithm is currently related to the system's <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/7bf610320e74dbfb92ff934022d0032d-1.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p style="max-width:90%"></p>authentication issues between multiple processes<p style="text-align: left;"><br></p><p style="text-align: left;">Note: <span style="color: #ff0000">Node.js <strong>Routing logic is not supported. Therefore, when designing applications, you should not rely too much on memory data </strong> objects </span> (such as </p>session<p style="text-align: left;">s and logins, etc.). Because each worker process is an independent process, they can be shut down or regenerated at any time as needed without affecting the normal operation of other processes. As long as there are worker processes alive, the server can continue to handle connections. If there are no surviving worker processes, existing connections are lost and new connections are rejected. Node.js does not automatically manage the number of worker processes, but the specific application should manage the process pool according to actual needs. <a href="http://www.php.cn/wiki/1498.html" target="_blank"><p style="text-align: left;">文档中已明确说明了,每一个工作进程都是独立的,并且互相之间除了能够进行通信外,没有办法共享内存。所以在设计鉴权的时候,有两种方法</p> <ol class=" list-paddingleft-2"> <li><p style="text-align: left;">通过共有的主进程存储鉴权信息,每次前端提交帐号密码,授权完成后,将 token 发送给主进程,下次前台<a href="http://www.php.cn/php/php-tp-demand.html" target="_blank">查询</a>时先在主进程获取授权信息</p></li> <li><p style="text-align: left;">通过统一的外部 redis 存取</p></li> </ol> <p style="text-align: left;">两种方法看来还是第二种好的不要太多,因此多进程的环境下,应该使用外部数据库统一存储 token 信息</p> <p style="text-align: left;"><span style="color: #ff0000"><strong>进一步的子进程间通信思考</strong></span></p> <p style="text-align: left;">虽然 node 中并没有直接提供的进程间通讯功能,但是我们可以通过主进程相互协调进程间的通讯功能,需要定义标准的通信格式,例如</p> <pre class="brush:php;toolbar:false">interface cmd { type: string from: number to: number msg: any }
这样通过统一的格式,主进程就可以识别来自各个进程间的通信,起到进程通信中枢的功能
egg.js 中 agent 的实现
+--------+ +-------+ | Master || Agent | +--------+ +-------+ ^ ^ ^ / | \ / | \ / | \ v v v +----------+ +----------+ +----------+ | Worker 1 | | Worker 2 | | Worker 3 | +----------+ +----------+ +----------+
我们看到 egg 在多进程模型之间实现了一个 agent 进程,这个进程主要负责对整个系统的定期维护
说到这里,Node.js 多进程方案貌似已经成型,这也是我们早期线上使用的方案。但后来我们发现有些工作其实不需要每个 Worker 都去做,如果都做,一来是浪费资源,更重要的是可能会导致多进程间资源访问冲突。举个例子:生产环境的日志文件我们一般会按照日期进行归档,在单进程模型下这再简单不过了:
每天凌晨 0 点,将当前日志文件按照日期进行重命名
销毁以前的文件句柄,并创建新的日志文件继续写入
试想如果现在是 4 个进程来做同样的事情,是不是就乱套了。所以,对于这一类后台运行的逻辑,我们希望将它们放到一个单独的进程上去执行,这个进程就叫 Agent Worker,简称 Agent。Agent 好比是 Master 给其他 Worker 请的一个『秘书』,它不对外提供服务,只给 App Worker 打工,专门处理一些公共事务。
这样我们可以指定一个进程作为 agent 进程,用于实现自己定义的事务。在 egg 中,主线程启动后 首先 fork agent进程,当 agent 进程启动完成后再启动具体的 worker 进程。参照上面的代码,相信这部分逻辑现在也不难实现了。这样 agent 就会获得 id 为1的进程
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to use cluster cluster in node. For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools
