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

What are the implementation methods of process communication in node

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the implementation methods of process communication in node". In daily operation, I believe that many people have doubts about the implementation of process communication in node. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the ways to achieve process communication in node?" Next, please follow the editor to study!

Communication actually covers all aspects of development, such as communication between client and server through various communication protocols, RPC communication, communication between various modules in the development process, communication between Electron main process and rendering process, and so on.

This paper mainly attempts to summarize the way of nodejs (single thread, multi-thread, multi-process) communication, usage scenario, implementation and so on.

The implementation of communication

General process communication is implemented as follows:

1. Shared Memory (memory sharing)

2. Socket (socket)

3. Pipe (unnamed pipe Pipe, named pipe FIFO)

4. Signal (signal)

5. Message queue (message queue)

Let's take a look at how to implement these forms of communication in node

1. Shared Memory (memory sharing)

Under a single machine (single thread in the client, multi-thread in a single process, and multi-processes in a single server), the way of communication through memory sharing is the most common.

Shared Memory (memory sharing)-single thread

From the operating system level, the memory of all threads in the process is shared, but only if you know the access address of the memory.

But from the language level (node or v8 implementation level), we do not directly touch the memory management, but indirectly from the syntax / api provided by V8 for memory operations. V8 provides us with three ways to share memory (perhaps more appropriately called shared variables): global variables, local variables, and shared parameters (call by sharing).

V8 translates the code into an abstract syntax tree through the Estree specification before executing it, and then interprets and compiles it. There is scope in the abstract syntax tree (see my other article on abstract syntax tree), while memory reading is looked up step by step through markers (variable naming). So if you need to share some memory between two methods, you can create it in their common scope.

Shared Memory (memory sharing)-Multithreading

In either the client environment or the node environment, we can implement multithreading in a similar way (node is implemented through worker_threads and browsers are implemented through Worker). The memory sharing here is mainly realized with the help of api (SharedArrayBuffer) of memory operation. Let's take a look at an example of a browser implementation:

/ / main thread const buffer = new SharedArrayBuffer (1024) const typedArr = new Int16Array (buffer) const newWorker = newWorker ('. / worker.js') typedArr [0] = 20newWorker.postMessage (buffer) newWorker.onmessage= (data) = > {console.group ('[the main thread]'); console.log ('Data received from the main thread:% iTunes, typedArr [0]); console.groupEnd () } / / Child threads addEventListener ('message', ({data}) = > {const arr = new Int16Array (data) console.group (' [the worker thread]') console.log ('Data received from the main thread:% iThreads, arr [0]) console.groupEnd () arr [0] = 18 postMessage (' Updated')}) / / result [the worker thread] Data received from the main thread: 20 [the main thread] Data received from the main thread: 18Shared Memory (memory sharing)-multiprocess

Because memory cannot be read from each other after process startup (system-level limitation), memory sharing between processes is actually achieved by opening up a new section of shared memory. However, node does not support shared memory for the time being and can only be implemented in low-level languages, such as the shared-memory-disruptor addon plug-in implemented by C++ (introduced in another article).

2. Socket (socket)

Socket can be implemented in two ways:

1 、 TCP Socket

2 、 UNIX Domain Socket

The main differences between the two are as follows:

TCP Socket is suitable for stand-alone, Cramp S architecture and so on. But UNIX Domain Socket is only suitable for stand-alone machines. UNIX Domain Socket does not need to go through a series of network transfers (protocols, subpackets, checks, etc.), and has higher performance and better stability. TCP Socket

Concept: TCP Socket is the middle abstract layer of the communication between the application layer and the TCP/IP protocol family, and it is an inter-process communication mechanism provided by the operating system.

TCP Socket communication should be one of the most common communication methods in our daily development (Cpact S architecture). The most common way in our daily development is the use of various application layer protocols (http,websocket,rpc,ftp, etc.). The http module in node is also based on net module.

Note: in fact, UDP also belongs to the TCP layer (not strictly referring to TCP communication, but the TCP/IP layer in the network communication layer). Node provides a 'dgram' module to implement it, but it has not been contacted in practical application, so it is not understood.

Net

In node, TCP Socket is implemented by the net module, and the net module mainly provides the following functions:

1. IPC support of the upper layer (actually the implementation of pipeline communication, which will be described in more detail later)

2. Net.Server class

/ / if the server creates a service through net.createServer, it will return a net.Server object, and you can listen for various events through the returned value. The port listens const net = require ('net') net.createServer ((server = > {server.end (`service!\ n`)})) .listen (3302, () = > {console.log (`running... `)})

3. Net.Socket class

Const net = require ('net') const socket = net.createConnection ({port: 3302}) socket.on (' data', data = > {console.log (data.toString ())}) UNIX Domain Socket

UNIX Domain Socket is through the creation of a file descriptor, communication between different processes by reading and writing this file descriptor for communication (can be divided into the creation process and other processes, communication between other processes can be created processes as a transit). E.g.

/ / create process const net = require ('net') const unixSocketServer = net.createServer (server = > {server.on (' data', data = > {console.log (`receive data: ${data}`)}) unixSocketServer.listen ('/ tmp/test', () = > {console.log ('listening...')}) / / other processes const net = require (' net') const socket = net.createConnection ({path:'/ tmp/test'}) socket.on ('data') Data = > {console.log (data.toString ())}) socket.write ('my name is vb') / / output result listening...receive data: my name is vb III, pipe

There are two kinds of pipeline communication, unnamed pipe and named pipe.

Unnamed pipes are implemented in the same way as UNIX Domain Socket by creating file descriptors for communication.

Named pipes communicate through fixed file descriptors:

"\. Pipe\\" + PIPE_NAME

For source code, please refer to stackoverflow (https://stackoverflow.com/questions/11750041/how-to-create-a-named-pipe-in-node-js).

At present, the understanding of pipeline communication is basically the same as that of UNIX Domain Socket, except that pipeline communication standardizes read and write permissions, half-duplex communication, and UNIX Domain Socket is freer.

4. Signal (signal)

Signal is a signal that the operating system sends to a process before it terminates. It can be realized in node through process.kill (pid, signal) / child_process.kill (pid, signal) interface, e.g.

/ / the http daemon to be terminated const Koa = require ('koa') const app = new Koa () app.listen (3004, () = > {console.log (`process pid is: ${process.pid} `) / / process pid is: 75208}) / / the operation process process.kill (75208,' SIGHUP') / / 'SIGHUP' is a signal to end the process generally For more other signals, please see [Identification] (https://blog.csdn.net/houjixin/article/details/71430489)).

But the premise here is that you need to get the terminated process pid, and you can read more about pid in my previous article on the process.

Message queue (message queuing)

At first I thought it was redis, various MQ and other TCP-based message queues. But in fact, it is a message queue in the operating system. Node does not provide the relevant upper layer interface for the time being, so it needs to be implemented at a lower level, e.g. Svmq

At this point, the study on "what is the implementation of process communication in node" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report