Node.js 我可以使用集群模块使函数处理程序发生在不同的进程中吗?
我正在用一组工人建立一个“经理”流程。我想使用Node.js 我可以使用集群模块使函数处理程序发生在不同的进程中吗?,node.js,multicore,Node.js,Multicore,我正在用一组工人建立一个“经理”流程。我想使用cluster()模块将工人分为不同的流程。由于工作者的消息处理可能是相当资源密集型的,因此将其推到不同的核心是有意义的 下面是管理器的伪代码: for i in worker_count create a new process (fork = cluster.fork()) create a worker and pass in the process (fork) to its constructor
cluster
()模块将工人分为不同的流程。由于工作者的消息处理可能是相当资源密集型的,因此将其推到不同的核心是有意义的
下面是管理器的伪代码:
for i in worker_count
create a new process (fork = cluster.fork())
create a worker and pass in the process (fork) to its constructor
in the worker
when the worker gets a "work" chunk
it should be processed on a core that is not the same as the manager
我找不到关于集群的太多信息,这些信息不仅仅是http.createServer等。我可以用不同的方法来实现这一点,但我想知道是否有办法获取集群的“上下文”,并在该上下文中放置函数 分支集群时,它会完全复制当前进程。有一个“主”工作进程,其余都创建为子进程。将工作传递给童工的一种方法是发送消息:
if (cluster.isMaster) {
// Spawn all your child workers here
var worker = cluster.fork();
// You could send a message to the worker from here if you wanted
worker.send('some message');
// You could loop through all the workers and send messages to them
for (var id in cluster.workers) {
cluster.workers[id].send('some message');
}
} else {
// This is where your child workers will branch into as they are not the master process
process.on('message', function (msg) {
if (msg === "some message") {
doSomeStuff();
}
});
}