使用集群和node-webworker构建高性能node.js应用程序

发布于 2024-12-03 10:58:40 字数 1000 浏览 3 评论 0原文

我不是node.js高手,所以我想对此有更多的看法。

我正在创建一个 HTTP node.js Web 服务器,它不仅必须处理大量并发连接,还必须处理长时间运行的作业。默认情况下,node.js 在一个进程上运行,如果有一段代码需要很长时间才能执行,则任何后续连接都必须等到该代码结束在前一个连接上执行的操作。

例如:

var http = require('http');
http.createServer(function (req, res) {

  doSomething(); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

所以我想使用 node-webworker 库在单独的线程中运行所有长时间运行的作业:

var http = require('http');
var sys = require('sys');
var Worker = require('webworker');
http.createServer(function (req, res) {

  var w = new Worker('doSomething.js'); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

并且为了使整个事情更加高效,我想还可以使用集群为每个CPU核心创建一个新的节点进程。

通过这种方式,我希望通过集群的不同进程来平衡客户端连接(假设在四核上运行的话有 4 个节点进程),然后在单独的线程上执行长时间运行的作业节点网络工作人员

这个配置有问题吗?

I'm not a node.js master, so I'd like to have more points of view about this.

I'm creating an HTTP node.js web server that must handle not only lots of concurrent connections but also long running jobs. By default node.js runs on one process, and if there's a piece of code that takes a long time to execute any subsequent connection must wait until the code ends what it's doing on the previous connection.

For example:

var http = require('http');
http.createServer(function (req, res) {

  doSomething(); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

So I was thinking to run all the long running jobs in separate threads using the node-webworker library:

var http = require('http');
var sys = require('sys');
var Worker = require('webworker');
http.createServer(function (req, res) {

  var w = new Worker('doSomething.js'); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

And to make the whole thing more performant, I thought to also use cluster to create a new node process for each CPU core.

In this way I expect to balance the client connections through different processes with cluster (let's say 4 node processes if I run it on a quad-core), and then execute the long running job on separate threads with node-webworker.

Is there something wrong with this configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南汐寒笙箫 2024-12-10 10:58:40

我看到这篇文章已经有几个月了,但我想在有人出现的情况下对此发表评论。

“默认情况下,node.js 在一个进程上运行,如果有一段代码需要很长时间才能执行,则任何后续连接都必须等到该代码结束在前一个连接上执行的操作。”

^-- 这并不完全正确。如果 doSomething();需要在发回响应之前完成,那么是的,但如果不是,您可以利用 Node.js 核心中可用的异步功能,并立即返回,而此项在背景。

通过在服务器中添加以下代码可以看到我所解释内容的一个简单示例:

setTimeout(function(){
    console.log("Done with 5 second item");
}, 5000);

如果您多次访问服务器,您将在客户端立即得到响应,并最终看到控制台充满消息发送响应后的秒数。

I see that this post is a few months old, but I wanted to provide a comment to this in the event that someone comes along.

"By default node.js runs on one process, and if there's a piece of code that takes a long time to execute any subsequent connection must wait until the code ends what it's doing on the previous connection."

^-- This is not entirely true. If doSomething(); is required to complete before you send back the response, then yes, but if it isn't, you can make use of the Asynchronous functionality available to you in the core of Node.js, and return immediately, while this item processes in the background.

A quick example of what I'm explaining can be seen by adding the following code in your server:

setTimeout(function(){
    console.log("Done with 5 second item");
}, 5000);

If you hit the server a few times, you will get an immediate response on the client side, and eventually see the console fill with the messages seconds after the response was sent.

青朷 2024-12-10 10:58:40

为什么不将代码复制并粘贴到文件中,然后通过 JXcore 运行它

$ jx mt-keep:4 mysourcefile.js

,看看它的执行情况。如果您需要真正的多线程而又不离开单线程的安全性,请尝试 JX。 100% node.JS 0.12+ 兼容。您可以生成线程并在每个线程中分别运行整个 Node.js 应用程序。

Why don't you just copy and paste your code into a file and run it over JXcore like

$ jx mt-keep:4 mysourcefile.js

and see how it performs. If you need a real multithreading without leaving the safety of single threading try JX. its 100% node.JS 0.12+ compatible. You can spawn the threads and run a whole node.js app inside each of them separately.

别在捏我脸啦 2024-12-10 10:58:40

您可能想看看 Q-Oper8,因为它应该为此类事情提供更灵活的架构。完整信息位于:

https://github.com/robtweed/Q-Oper8

You might want to check out Q-Oper8 instead as it should provide a more flexible architecture for this kind of thing. Full info at:

https://github.com/robtweed/Q-Oper8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文