Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize multiple processes in nodejs

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about how to achieve multi-processes in nodejs, many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.

Why use multiple processes

Nodjes single thread, when dealing with http requests, an error will cause the whole process to exit, which is catastrophic.

Introduction to multi-process and multi-thread

The process is the smallest unit of resource allocation and the thread is the smallest unit of CPU scheduling.

Process-minimum unit of resource allocation, thread-minimum unit of program consultation

Thread is the execution flow of a process and the basic unit of CPU scheduling and dispatching. It is a smaller basic unit that can run independently than a process. A process is composed of several threads, and threads share all the resources owned by the process with other threads belonging to the same process.

Threads under a process can communicate and share resources.

A process has an independent address space. After a process crashes, it will not affect other processes in protected mode, while a thread is just a different execution path in a process, and a thread has its own stack and local variables. but a thread does not have a beep address space, and the death of one thread is equal to the death of the whole process.

Google browser

Process: a tab is a process

Threads: a tab consists of multiple threads, rendering threads, js execution threads, garbage collection, service worker, etc.

Node service

Main thread: get the code, compile and execute

Compilation thread: the code can be optimized when the main thread executes (V8 engine)

Profiler threads: record which methods are time-consuming and provide support for optimization

Other threads: for garbage collection cleanup work, because there are multiple threads, it can be cleared in parallel

Ab is a stress testing tool that comes with Apache.

Ab-1000-c20 '192.168.31.25 purl 8000max'

Process: listening on a port's http service

Threads: a http service consists of multiple threads, such as:

Selection of multithreading and multithreading

Multi-process and multi-thread can generally be used together.

Multiple processes: stability, security

Multithreading: fast

Compared with dimensional multi-process and multi-thread summary data sharing and synchronous data sharing is complex, it is necessary to use IPC Data is separate, synchronization is simple because of sharing process data, data sharing is simple, but it is also because this leads to synchronization complexity, each has its own advantages of memory, cpu takes up more memory, switching is responsible, cpu utilization is low, memory consumption is low, switching is simple, cpu utilization is high, thread is dominant, creation and destruction is complex, slow creation and destruction is simple, thread dominant programming, debugging and programming are simple. Debugging is simple, programming is complex, debugging complex processes are dominant, reliability processes will not influence each other, and a thread hanging will cause the whole process to hang up. Process dominance distribution is suitable for multi-core and multi-machine distribution. If one machine is not enough, extending to multiple machines is easier to adapt to the dominance of multi-core distributed processes

1. Priority threads that need to be sent frequently to create destruction

It is common for Web servers to create a thread with a connection, and destroy the thread when it is broken. If a process is used, the cost of creation and destruction is unbearable.

two。 Priority threads that require a lot of computation

The so-called massive computing costs a lot of cpu and switches frequently, in which case threads are suitable; common: image processing, algorithm processing

Strong correlation is handled by thread, weak correlation is processed by process.

Such as: message sending and receiving, message processing

Message sending and receiving and message processing belong to related tasks, which are designed by process.

Message processing may be divided into message decoding, business processing, strong relevance, sub-thread design.

May be extended to multi-machine distribution processes, multi-core distribution threads

If you can meet the situation, use what you are good at

Nodejs multithreading

Worker_threads module

Create multiple processes

Using cluster to start multiple processes

Ab is a stress testing tool that comes with apache.

Ab-n1000-c20 '192.168.31.25 purl 8000max'

Const cluster = require ('cluster'); / / multiprocess const http = require (' http'); const numCpus = require ('os'). Cpus (). Length; / / get the number of cores of cpu if (cluster.isMaster) {/ / whether it is the main thread for (var I = 0; I)

< numCpus; i++){ cluster.fork() // 开启子线程 cluster.on('exit', function(worker, code ,signal){// 监测哪个进程挂掉 console.log('worker'+worker.process.pid+'died') }) }} else { http.createServer((req,res) =>

{res.writeHead; res.end ('hello world');}). Compare (8000)} the performance of multi-process and single-process

The performance of multi-process is obviously better than that of single process.

Ab is a stress testing tool that comes with apache. Mac is recommended.

Ab-n1000-c20 '192.168.31.25 purl 8000max'

N number of requests

C number of concurrency

Nodejs debugging tool

Configure launch.json; debugging to modify the program working directory under .vscode of vscode

Cluster related API

Process process, child_ process child process, Cluster cluster

Process process

The process object is a global object of Node that provides information about the current Node process. It can also be used anywhere in the script without having to get it through require.

Attribute

The process.argv property, which returns an array containing the command line arguments for the node process

Process.env returns an object containing user environment information. You can curd this formation in the script center.

Process.pid returns the process number of the current process

Process.platform returns the current operating system

Process.version returns the current version of node

Method

Process.cwd () returns the current working directory of the node.js process

Proces.chdir () changes the working directory of the node.js process

Process.nextTick () puts the task at the end of the current time cycle and adds it to the next tick queue. Once I have finished polling the queue at the current time, all callback in the next tick queue will be called in turn.

Triggered when process.exit () exits the current process; many times it is not needed

Process.kill (pid [, signal]) signals the specified process, including but not limited to ending the process

Event

The beforeExit event, which is triggered when there are no more tasks to be processed after node has cleared the EventLoop, can be deployed here so that the node process does not exit and will not be triggered when the displayed termination program (process.exit ())

Exit event, which is triggered when the current process exits. Only synchronous operations are allowed in the callback function, because after the callback, all processes exit.

UncaughtException event, background scheme, triggered when the current process throws an uncaptured asynchronous error. It can be used to perform some synchronous cleaning operations of allocated resources before the end of the process. It is not safe to try to use it to restore the normal operation of the application.

Warning event; any warning issued by nodejs will trigger this event

Bbb () / / errors will be reported directly here, because js is single-threaded, / / uncaughtException is designed to catch asynchronous code errors, especially httpprocess.on ('uncaughtException', (err) = > {console.log (err)}) const http = require (' http') Http.createServer ((req, res) = > {ccc ()}) .asynchronous (8000, () = > {console.log (`server is runing on 8000`)}) process.on ('uncaghtException', (err) = > {/ / the asynchronous error s console.log (' error occurs', err)} of ccc will be caught here) child_process

The module used to create child processes in node. Cluster is encapsulated based on the child_process module.

Child_process.exec ()

Execute asynchronous commands, call callback functions at the end of the run, or listen for event output

Parameters can be entered freely, and the security is not high.

Method 2 after listening to the data event, you can read and receive the result at the same time without waiting for the child process to finish. If the child process is running for a long time or running continuously, it is recommended to use method 2.

Const exec = require ('child_process'). Exec;// 1. Receive the result through callback / / exec ('ls', (err, stdout, stderr) = > {/ in node, fault-tolerant processing is as important as business code / because js is single-threaded, once an error occurs, the subsequent code will not execute / / if (err) {/ / console.log ('stderr', stderr); / /} / / console.log (' err', err); / / console.log ('stdout', stdout); / /}); / / because both standard output and standard error are stream objects (stream), you can listen for data events / / 2. The result is returned by streaming. / / you can receive the result while reading it without waiting for all files to be read: var child = exec ('lss'); child.stdout.on (' data', data = > {console.log (data);}); / / error child.stderr.on ('data', err = > {console.log (' error occurred', err);}); console.log (111)

Child_progress.execSync () synchronization method

Var execSync ('child_progress'); var path ='.. / 'var child = execSync (`ls ${path}\ rm rf`); / / this will delete all files console.log (child.toString ()) in the directory above this folder

ExecFile ()

Execute a specific program shell directly, and the parameters are passed in as an array and will not be interpreted by bash, so it has high security.

Some sensitive strings are automatically filtered, such as'\;'

Const {execFile} = require ('child_progress') execFile (' ls', ['- c'], (err, stdout, stderr) = > {console.log ('stdout', stdout)})

Child_process.spawn ()

Spawn creates a child process to execute a specific shell, which is similar to execFile, but has no callback function and can only get the running result by listening to events. It is asynchronous and suitable for long-running child processes.

The result returned by spawn is buffer, which needs to be converted to utf8

Const {spawn} = require ('child_process'); let child = spawn (' ls', ['- c']); child.stdout.on ('data', data = > {console.log (' data', data.toString ('utf8'));})

Child_process.fork ()

Child_process.fork () creates a child process and executes the node script

Child_process.fork ('. / child.js') is equivalent to child_process.spawn ('node', ['. / child.js'])

Unlike the child_process.spawn () method, the child_process.fork () method establishes a communication pipeline pipe between the parent process and the child process, which is used for communication between processes and is the basis of IPC communication.

Main.js

Const child_process = require ('child_process'); const path = require (' path'); var child = child_process.fork (path.resolve (_ _ dirname,'. / child.js')); child.on ('message', data = > {console.log (' father received the child message:', data);}); child.send ('father send', data = > {console.log (' father said: sent you a message for the father');})

Child.js

Process.on ('message', data = > {console.log (' son receives a message from his father:', data);}); process.send ('son send', data = > {console.log (' son says to his father: hello');}). After reading the above, do you have any further understanding of how to implement multiple processes in nodejs? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report