在是否Node.js的异步编程加快CPU密集型任务? [英] Does asynchronous programming in node.js speed up CPU-bound tasks?

查看:125
本文介绍了在是否Node.js的异步编程加快CPU密集型任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候,我回答与<一个问题href=\"http://stackoverflow.com/questions/17201450/salt-and-hash-password-in-nodejs-w-crypto/17201754#17201754\">this答案。在这个例子中,我贴,我用了bcrypt节点模块中呼叫的同步版本。我选择使用,主要是因为我认为它做出的反应看起来有点清洁呼叫的同步版本,但我也没有想到,因为bcrypt是CPU和内存密集型而不是I / O密集​​型会影响性能。这是我的理解是节点运行在单个线程几乎所有code喜欢的浏览器做的,只用于后台线程的东西如I / O和数据库访问。这使我相信,CPU密集型任务仍然基本上是块的服务器,因为没有其他线程来卸载工作。

Earlier today I responded to a question with this answer. In the example that I posted, I used the synchronous version of a call in the bcrypt node module. I chose to use the synchronous version of the call primarily because I thought it made the response look a little cleaner, but I also didn't think that it would affect performance because bcrypt is cpu and memory intensive instead of I/O bound. It was my understanding that node ran almost all your code on a single thread like browsers do, and only used background threads for things like I/O and database access. This lead me to believe that cpu-intensive tasks would still essentially "block" the server, since there were no other threads to offload the work to.

在我的回应注释表明,我的假设是错误的,一些研究后,我意识到,我真的我以前不有node.js的如何处理这样的事情有很大的把握。 在是否Node.js的异步编程加快cpu和内存密集型的电话?如果是这样,它是如何做到的?

A comment on my response indicated that my assumption was wrong, and after some research I realized that I did't really have a great grasp on how node.js handles this sort of thing. Does asynchronous programming in node.js speed up cpu and memory intensive calls? If so, how does it do it?

推荐答案

这取决于模块是如何实现的。

It depends on how the module is implemented.

如果模块没有任何支持,实施了螺纹然后是,CPU限制处理不能异步完成。有些功能提供回调和我的外表异步但他们真的不。他们实际上同步运行,并阻止事件循环。在JavaScript这样的例子是 Array.forEach()

If the module is implemented without any support for threading then yes, CPU bound processing cannot be done asynchronously. Some functions provide callbacks and my look asynchronous but they're really not. They actually run synchronously and blocks the event loop. Examples of this in javascript is Array.forEach().

但是,模块可被实现为执行的处理在后台线程。在这种情况下,它确实是异步的,可以加快CPU密集型任务。至少,它释放了事件循环处理传入的请求,而后台线程忙于计算结果。

But, modules may be implemented to do the processing in background threads. In which case it truly is asynchronous and can speed up CPU bound tasks. At the very least it frees up the event loop to handle incoming requests while the background thread is busy computing results.

这方面的一个例子是 crypto.pbkdf2()在节点自己的加密模块的功能。

An example of this is the crypto.pbkdf2() function in node's own Crypto module.

但是,如何能执行模块在其他线程code node.js的时候在一个线程中运行?

But, how can modules execute code in other threads when node.js runs in a single thread?

这是实施的原始的方式只是该模块没有用JavaScript编写的,但被代替用C / C ++和接口到node.js的通过它的插件API。

The original way this was implemented was simply that the module wasn't written in javascript but was instead written in C/C++ and interfaced to node.js via it's addons API.

但这些天,即使纯JavaScript模块和功能可生成线程和/或进程。节点有一个名为集群的实验模块,设置节点进程的主/从集群。然后,您的模块或code可以运行工作进程释放主节点进程来处理事件循环势必任务CPU。也有对新公共管理提供了一些线程模块。只要搜索上npmjs.org线程。

But these days even pure javascript modules and functions can spawn threads and/or processes. Node has an experimental module called Cluster that sets up a master/slave cluster of node processes. Your module or code can then run the CPU bound task in a worker process freeing up the main node process to handle the event loop. There are also several threading modules available on npm. Just search for "thread" on npmjs.org.

所以,可以做成是CPU密集型任务的运行速度,或者至少不是异步运行阻塞主事件循环。

So, yes CPU bound tasks can be made to run faster or at least not block the main event loop by running asynchronously.

这篇关于在是否Node.js的异步编程加快CPU密集型任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆