简体   繁体   English

node.js用于cpu密集型操作

[英]node.js for cpu intensive operations

There is something that I'm really not understanding about Node.js: pretty much everywhere you can read that node.js is not recommended for HPC (high performance computing) due to his async but single-thread nature. 有一些我真的不了解Node.js:几乎无处不在,你可以读到node.js不建议用于HPC(高性能计算),因为他的异步但是单线程性质。

You can find node.js pretty much always explained with Express.js to build some really fast web-server or service that also allows you to send HTML or JSON in your response after some query to an SQL or NoSQL database. 您可以找到node.js,它总是用Express.js解释,以构建一些非常快速的Web服务器或服务,它还允许您在对SQL或NoSQL数据库进行一些查询后在响应中发送HTML或JSON。

But here the thing. 但在这里的事情。

You can also find on npm lots of packages build for time consuming and intensive operations , like fluent-ffmpeg for video encoding. 您还可以在npm上找到许多用于耗时和密集操作的软件包,例如用于视频编码的fluent-ffmpeg Or you can use request and cheerio and build a web scraper. 或者您可以使用请求cheerio并构建Web scraper。

Npm in also full of command line application written for node.js (in node.js). Npm也充满了为node.js编写的命令行应用程序 (在node.js中)。 Are all the application for non-time-consuming operations? 是否所有申请都是非耗时的操作?

Also we can find a lot of frameworks, like next.js that, at least to me, doesn't seem like they are doing something so easy . 我们也可以找到很多框架,比如next.js ,至少在我看来,他们似乎并没有做那么容易的事情

So, what can we do with node.js? 那么,我们可以用node.js做些什么呢?

What does "cpu intensive operations" really mean? “cpu密集型操作” 究竟意味着什么?

I love using node and javascript to build web-servers, service and command line applications too, but sometimes I feel like I did not understand the real potential and the real limits of node.js. 我喜欢使用node和javascript来构建web服务器,服务和命令行应用程序,但有时我觉得我不理解node.js的真正潜力和真正的限制。

If you look closely at the ffmepeg package , you'll note that it says: 如果仔细观察ffmepeg包 ,你会注意到它说:

In order to be able to use this module, make sure you have ffmpeg installed on your system 为了能够使用此模块,请确保在系统上安装了ffmpeg

This is a hint as to what's going on in this case. 这是一个暗示在这种情况下发生了什么。 This package is not reimplementing the entirety of ffmpeg, but instead simply serving as an API to an existing ffmpeg installation. 此软件包不会重新实现整个ffmpeg,而只是作为现有ffmpeg安装的API。

If you look at the code , you can see that it's actually just spawn ing a copy of ffmpeg to do the work. 如果你查看代码 ,你会发现它实际上只是spawn了一个ffmpeg副本来完成这项工作。 This therefore isn't actually running "in node". 因此,这实际上并不是“在节点中”运行。

So that's ffmpeg, what about your other examples? 这就是ffmpeg,你的其他例子呢? Well, I suspect most of them aren't as CPU heavy as you might think - after all, the entire design of many, many node applications is to deal with HTML and webpages, and a scraper isn't something that takes a lot of processing power to do. 好吧,我怀疑它们中的大多数并不像你想象的那么重 - 毕竟,很多很多节点应用程序的整个设计都是处理HTML和网页,刮板不需要很多东西。处理能力。

So, "What does "cpu intensive operations" really mean?" 那么,“什么”cpu密集型操作“真正意味着什么?” is a pretty subjective one. 是一个非常主观的。 Some things to note from your source link and real life: 您的源链接和现实生活中需要注意的一些事项:

  1. The copyright at the bottom of the page is 2011. That's ancient in javascript development time. 页面底部的版权是2011年。这在javascript开发时间很古老。 This advice was written before many iterations and innovations happened. 这个建议是在许多迭代和创新发生之前编写的。 It's likely not wholly wrong, but it's missing the current point of view we have. 它可能并非完全错误,但它缺少我们目前的观点。

  2. CPU-heavy applications are called out in comparison to their I/O: 与I / O相比,CPU调用大量应用程序:

    very heavy on CPU usage, and very light on actual I/O CPU使用率非常高,实际I / O非常轻

    Web scrapers are probably not considered "light on actual I/O" Web刮刀可能不被认为是“实际I / O的亮点”

  3. This is a subjective choice. 这是一个主观的选择。 No one can dictate exactly how you should be implementing your application. 没有人能够确切地说明你应该如何实现你的应用程序。 If they were, they'd be writing it, not you. 如果他们是,他们会写它,而不是你。

  4. The real world is not strictly defined into "CPU-intensive" and not. 现实世界并没有严格定义为“CPU密集型”而不是。 Many applications start with some requirements that look great for node, and then later, some get added that aren't as perfect as a fit, or are even a bad fit. 许多应用程序从一些看起来很适合节点的要求开始,然后,一些应用程序的添加不是那么完美,甚至不合适。 Real world teams can't always reinvent everything whenever a new requirement gets added, so shims like the mentioned ffmpeg package get created. 无论何时添加新需求,真实世界的团队都不能总是重新发明所有内容 ,因此会像创建的ffmpeg包那样使用填充程序。

So how do you know the limits? 那你怎么知道极限呢? Again, this is a subjective choice. 同样,这是一个主观的选择。 It's fair to set some hard boundaries, like video encoding, as things that really should not be done in pure javascript. 将视频编码等一些硬边界设置为真正不应该在纯JavaScript中完成的事情是公平的。 But the space from there to a simple API gets pretty murky depending on the exact requirements and details. 但是从那里到一个简单的API的空间变得非常模糊,具体取决于具体的要求和细节。 If it works and is reasonably performant, it's probably ok! 如果它有效并且性能合理,那可能还可以! You might get more performance out of another system, but you might also lose your knowledge of the ecosystem and integration with the community. 您可能会从另一个系统中获得更多性能,但您可能也会失去对生态系统的了解以及与社区的集成。

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

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