简体   繁体   English

Node.js 是否适合构建大型文件共享 Web 应用程序?

[英]Is Node.js appropriate for building a large file sharing web app?

I'm a beginner programmer and I am taking an online course in Node.js and the instructor said not to use Node.js to build CPU-intensive apps.我是一名初级程序员,我正在学习 Node.js 的在线课程,讲师说不要使用 Node.js 来构建 CPU 密集型应用程序。 I don't know what is considered CPU-intensive.我不知道什么被认为是 CPU 密集型的。 I am planning on building a large file sharing web app, but I don't know if Node.js is the right tool to get the job done.我计划构建一个大型文件共享 Web 应用程序,但我不知道 Node.js 是否是完成工作的正确工具。

Node.js has an event-driven architecture, meaning it has a built-in event loop and a asynchronous I/O API. Node.js 有一个事件驱动的架构,这意味着它有一个内置的事件循环和一个异步 I/O API。

CPU-intensive tasks are those which use a lot of CPU computation in order to complete, such as finding prime numbers, sorting strings, et cetera. CPU 密集型任务是那些使用大量 CPU 计算才能完成的任务,例如查找素数、排序字符串等。

When a CPU-intensive task takes place in Node.js it will block the event loop until its completion.当 Node.js 中发生 CPU 密集型任务时,它将阻塞事件循环,直到它完成。 During this time the program will be unable to process new incoming messages.在此期间,程序将无法处理新传入的消息。

Conversely, I/O operations doesn't need that much of CPU usage.相反,I/O 操作不需要那么多 CPU 使用率。

Since generally speaking web apps and file sharing involves more I/O operations than CPU-intensive tasks (such as reading a file from disk and sending its contents through a network socket), Node.js can be well suited to create a large file sharing web app.由于一般来说 Web 应用程序和文件共享涉及比 CPU 密集型任务(例如从磁盘读取文件并通过网络套接字发送其内容)更多的 I/O 操作,因此 Node.js 非常适合创建大型文件共享网络应用程序。

Is Node.js appropriate for building a large file sharing web app? Node.js 是否适合构建大型文件共享 Web 应用程序?

Yes.是的。

Modern node.js has multiple ways to handle CPU-intensive tasks these days so that advice is perhaps a bit dated.如今,现代 node.js 有多种方法来处理 CPU 密集型任务,因此这些建议可能有点过时了。 The advice comes from the fact that it runs your main Javascript in a single thread and relies on asynchronous I/O to allow the single thread to very efficiently handle lots of requests using asynchronous I/O.该建议来自这样一个事实,即它在单个线程中运行您的主 Javascript,并依赖于异步 I/O 以允许单线程使用异步 I/O 非常有效地处理大量请求。

But, if some of your request handlers are using a lot of CPU cycles, then it bogs down the event loop.但是,如果您的某些请求处理程序使用了大量 CPU 周期,那么它就会陷入事件循环。

But, modern node.js has the ability to:但是,现代 node.js 能够:

  1. Cluster it using the built-in cluster module so you have multiple node.js processes taking requests in a load balanced fashion that will involve many CPUs.使用内置的集群模块对其进行集群,这样您就有多个 node.js 进程以负载平衡的方式处理请求,这将涉及许多 CPU。

  2. Use Worker Threads to start up individual threads where you can run CPU-intensive tasks and not block the main event loop.使用工作线程启动单个线程,您可以在其中运行 CPU 密集型任务而不阻塞主事件循环。

  3. Create some sort of work queue where you can use either additional processes or worker threads to CPU crunch on queued up work.创建某种工作队列,您可以在其中使用其他进程或工作线程来处理排队工作的 CPU 紧缩。 I could imagine an image processing application doing it like this.我可以想象一个图像处理应用程序会这样做。

In your case, a file-sharing application probably doesn't even involve much CPU intensive stuff as most likely the main logic about who can access what is not CPU intensive and the act of serving shared resources is entirely I/O bound (something node.js is very efficient at).在您的情况下,文件共享应用程序甚至可能不涉及太多 CPU 密集型的东西,因为最有可能的主要逻辑是关于谁可以访问非 CPU 密集型的内容以及提供共享资源的行为完全是 I/O 绑定(某个节点.js 非常有效)。 If, once you got into your development, you found a CPU-intensive bottleneck somewhere, you could very easily work around it with one of the above approaches.如果进入开发阶段后,发现某个地方存在 CPU 密集型瓶颈,则可以使用上述方法之一轻松解决该问题。

And, there are many ways to horizontally scale node.js with clustering on a single server or multiple server clusters.而且,有很多方法可以在单个服务器或多个服务器集群上使用集群水平扩展 node.js。 If your app is I/O bound because of all the shared content, your scaling is probably mostly about network bandwidth and disk I/O scaling, not about the actual app logic or the CPU.如果您的应用因所有共享内容而受到 I/O 限制,那么您的扩展可能主要与网络带宽和磁盘 I/O 扩展有关,而不是与实际应用逻辑或 CPU 相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM