.net中的Node.js与Async/await [英] Node.js vs Async/await in .net

查看:50
本文介绍了.net中的Node.js与Async/await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释/重定向我,Node.js的异步模型(非阻塞线程)与任何其他语言(例如c#处理I/O的异步方式)之间有什么区别?在我看来,两者都是相同的模型.请提示.

Can someone explain/ redirect me, what is the difference between Node.js's async model(non blocking thread) vs any other language for example c#'s asynchronous way of handling the I/O. This looks to me that both are same model. Kindly suggest.

推荐答案

两个模型都非常相似.有两个主要区别,其中一个即将消失(对于很快"的定义).

Both models are very similar. There are two primary differences, one of which is going away soon (for some definition of "soon").

一个区别是Node.js是异步单线程的,而ASP.NET是异步多线程的.这意味着Node.js代码可以做一些简化的假设,因为 all 您的代码始终在相同的线程上运行.因此,当您的ASP.NET代码为await s时,它可能可以在不同的线程上恢复,并且您有责任避免诸如线程本地状态之类的事情.

One difference is that Node.js is asynchronously single-threaded, while ASP.NET is asynchronously multi-threaded. This means the Node.js code can make some simplifying assumptions, because all your code always runs on the same exact thread. So when your ASP.NET code awaits, it could possibly resume on a different thread, and it's up to you to avoid things like thread-local state.

但是,相同的区别也是ASP.NET的优势,因为这意味着async ASP.NET可以直接扩展到服务器的全部功能.如果您考虑使用8核计算机,则ASP.NET可以同时处理8个请求(的同步部分).如果将Node.js放在功能强大的服务器上,通常会实际运行8个单独的Node.js实例,并添加诸如nginx或简单的自定义负载均衡器之类的东西来处理该服务器的路由请求.这也意味着,如果您希望在服务器范围内共享其他资源(例如,缓存),则还需要将它们移出进程.

However, this same difference is also a strength for ASP.NET, because it means async ASP.NET can scale out-of-the-box up to the full capabilities of your sever. If you consider, say, an 8-core machine, then ASP.NET can process (the synchronous portions of) 8 requests simultaneously. If you put Node.js on a souped-up server, then it's common to actually run 8 separate instances of Node.js and add something like nginx or a simple custom load balancer that handles routing requests for that server. This also means that if you want other resources shared server-wide (e.g., cache), then you'll need to move them out-of-proc as well.

另一个主要差异实际上是语言差异,而不是平台差异. JavaScript的异步支持仅限于回调和Promise,即使您使用最好的库,当您执行不重要的事情时,您仍然会得到真正尴尬的代码.相比之下,C#/VB中的async/await支持使您可以编写非常自然的异步代码(更重要的是 maintainable 异步代码).

The other major difference is actually a difference in language, not platform. JavaScript's asynchronous support is limited to callbacks and promises, and even if you use the best libraries, you'll still end up with really awkward code when you do anything non-trivial. In contrast, the async/await support in C#/VB allow you to write very natural asynchronous code (and more importantly, maintainable asynchronous code).

但是,语言差异正在消失. JavaScript的下一修订版将引入生成器,生成器(以及帮助程序库)将使用与现在一样自然的使用async/await在Node.js中生成异步代码.如果您现在想使用即将推出"的东西,则在V8 3.19中添加了生成器,并将其生成到Node.js 0.11.2(不稳定分支)中.传递--harmony--harmony-generators以显式启用生成器支持.

However, the language difference is going away. The next revision of JavaScript will introduce generators, which (along with a helper library) will make asynchronous code in Node.js just as natural as it is today using async/await. If you want to play with the "coming soon" stuff now, generators were added in V8 3.19, which was rolled into Node.js 0.11.2 (the Unstable branch). Pass --harmony or --harmony-generators to explicitly enable the generator support.

这篇关于.net中的Node.js与Async/await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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