NODJS Multi-thread master-slave mode

Source: Internet
Author: User

# #work. js

Let HTTP = require (' http ');

Let port = Math.Round ((1+math.random ()) *1000);
Http.createserver (function (req, res) {
Console.log (PROCESS.PID);
Res.writehead (200,{' content-type ': ' Text/plain ');
Res.end (' Hello World ');
}). Listen (Port,function () {
Console.log ('----Server start port: ' +port ');
});

# # Master.js
Let fork = require (' child_process '). Fork;

Let CPUs = require (' OS '). CPUs;
Console.log (cpus.length);
Replicate the number of node processes based on the number of CPUs on the current machine
for (let i=0;i<2;i++) {
Fork ('./work.js ');
}

1, start the work.js first, and then start the master.js. You can see the boot work.js when listening to a port, and then start the master.js when the thread listens to the other two ports.
You can start a certain thread service by detecting the number of CPUs in the current system.

NODJS Multi-thread master-slave mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.