In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve brotherly process communication in nodejs. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
problem
Although we can save an instance of the worker process in the main process, it is still very troublesome to complete the communication between the worker process and process A through the main process, as follows
1 first of all, the main process should monitor the tasks sent by each worker process.
2 then pass the task to process A.
3 after process An is finished, notify the main process, and the main process sends the result to the child process corresponding to the task, where the worker process needs to save the context corresponding to the task (such as callback). Because the worker process may send multiple tasks to the main process at the same time, when the main process notifies the worker process that a task is completed, the worker process needs to find the corresponding context through the task and then proceed to the next step. Such as performing a callback.
Solution
Open a service in the main process to implement inter-process communication without inheritance relationship. The selected mode of inter-process communication is Unix domain. Tcp is not selected because the inter-process communication with the host is too heavy and inefficient to use tcp (need to packet and unpack through the protocol stack). The child process can communicate with the main process through this service, and then the main process forwards the request to the child process that handles CPU-type tasks. The structure is as follows: the Unix domain service is enabled in the main process instead of process A because when you add child processes that deal with other tasks later, you can reuse the Unix domain service and act as an api gateway. But if there is one more layer, there will be more communication costs. More directly, you can use the following structure
Concrete realization
Client
Const net = require ('net'); const {EventEmitter} = require (' events')
Class Work extends EventEmitter {}
Class UnixDomainClient extends EventEmitter {constructor (options) {super (); this.options = options;} send (data) {const work = new Work (); const socket = net.connect (this.options.path); socket.end (JSON.stringify (data)); socket.on ('error', (e) = > {work.emit (' error', e);}); let res = null Socket.on ('data', (chunk) = > {res = res? Buffer.concat ([res, chunk]): chunk;}); socket.on ('end', () = > {work.emit (' message', res & & res.toString ();}); return work;}} const work = new UnixDomainClient ({path:'/ tmp/test.sock'}). Send ('hello'); work.on (' message', function (res) {console.log (res);})
Server
Const fs = require ('fs'); const net = require (' net'); const constants = {UNIX_PATH:'/ tmp/test.sock',} if (fs.existsSync (constants.UNIX_PATH)) {fs.unlinkSync (constants.UNIX_PATH);} const server = net.createServer ({allowHalfOpen: true}, (client) = > {let data = null; client.on ('data', (chunk) = > {data = data? Buffer.concat ([data, chunk]): chunk;}); client.on ('end', () = > {console.log (`recive msg: ${data.toString ()} `) client.end (' world');}); server.listen (constants.UNIX_PATH, () = > {console.log (`bind uinx path ${constants.UNIX_PATH}`);}) Server.on ('error', (error) = > {console.log (`unix domain server error ${error.toString ()}`); process.on (' exit', () = > {if (fs.existsSync (constants.UNIX_PATH)) {fs.unlinkSync (constants.UNIX_PATH);}}). After reading the above, do you have any further understanding of how to implement sibling process communication 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.