Ruby EventMachine、Python Twisted 或 JavaScript Node.js 的要点/目的是什么?

发布于 2024-09-04 03:42:16 字数 273 浏览 10 评论 0原文

我不明白这些框架解决什么问题。它们是 Apache HTTPD、Tomcat、Mongrel 等 HTTP 服务器的替代品吗?或者他们更多?为什么我可以使用它们......一些现实世界的例子?我见过无数的聊天室和广播服务的例子,但没有看出这与设置一个 Java 程序来打开套接字并为每个请求分派一个线程有什么不同。

我想我了解非阻塞 I/O,但我不明白它与多线程 Web 服务器有什么不同。对于 Node.js,我读到它只有一个线程,这可能比处理多个线程更有效,但这是这些框架与传统 Web 服务器之间的唯一区别吗?

I don't understand what problem these frameworks solve. Are they replacements for a HTTP server like Apache HTTPD, Tomcat, Mongrel, etc? Or are they more? Why might I use them... some real world examples? I've seen endless examples of chat rooms and broadcast services, but don't see how this is any different than, for instance, setting up a Java program to open sockets and dispatch a thread for each request.

I think I understand the non-blocking I/O, but I don't understand how that is any different than a multi-threaded web server. For Node.js I read that it only has a single thread, and that this may be more efficient than juggling multiple threads, but is that the only difference between these frameworks and a traditional web server?

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

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

发布评论

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

评论(3

姐不稀罕 2024-09-11 03:42:16

如果您想编写网络代码,您可以使用这些框架之一。

例如,如果您要编写一款大型多人视频游戏,“设置 Java 程序...为每个请求分派一个线程”可能不是一个选择;处理许多线程非常复杂,而且性能也很差。更不用说“只是生成一堆线程”这一事实缺少 Twisted 等人的一堆管理工具。等人。有,如 twistd,它处理日志记录、守护进程、启动和关闭等。

或者,如果您想编写构建自动化系统,则可以异步调用和控制子流程 会很有用。如果异步生成一个进程,您可以轻松终止该进程并优雅地处理其退出。如果您通过启动一个线程并在该线程中阻塞来生成它,您将无法轻松停止它,因为 停止线程本质上是不安全的

EventMachine 和 Twisted 都可以用来编写客户端程序;也许您正在编写一个不基于 Web 的 GUI 应用程序,并且您希望在客户端和服务器上使用相同的协议实现。

由于您可以在许多不同的上下文中使用异步框架,因此您可能想在 Web 应用程序中使用它,只是因为您有现有的库代码,这些代码是为使用您想要使用的异步框架的其他应用程序编写的。或者您可能希望能够在某些假设的未来非 Web 应用程序中重用您的 Web 应用程序代码。在这种情况下,就功能而言,它与使用 Apache 或 Tomcat 或其他任何语言没有太大区别,它只是为您提供了一种更通用、可重用的方式来组织程序。

You might use one of these frameworks if you want to write code that does networking.

For example, if you were going to write a massively multiplayer video game, "setting up a Java program ... to dispatch a thread for each request" probably isn't an option; juggling that many threads is phenomenally complex, and it performs poorly as well. Not to mention the fact that "just spawn a bunch of threads" is missing a bunch of the management tools that Twisted et. al. have, like twistd, which handles logging, daemonization, startup and shutdown, and so on.

Or if you wanted to write a build automation system, the ability to asynchronously invoke and control subprocesses would be useful. If you spawn a process asynchronously, you can easily kill that process and gracefully deal with its exit. If you spawn it by starting a thread and blocking in that thread you can't stop it easily, since stopping a thread is inherently unsafe.

EventMachine and Twisted can both be used to write client-side programs as well; maybe you're writing a GUI application that isn't web-based, and you want to use the same protocol implementation on the client and the server.

Since you can use asynchronous frameworks in so many different contexts, it's possible that you might want to use it in a web application simply because you have existing library code, written for some other application using your async framework, which you want to use. Or you might want to be able to re-use your web application code in some hypothetical future non-web application. In this case, it's not that much different than using Apache or Tomcat or whatever in terms of functionality, it just gives you a more general, re-usable way to organize your program.

趴在窗边数星星i 2024-09-11 03:42:16

事实上,基于事件的框架适合有大量 io 和较少 cpu 操作的情况,但这定义了大多数网页,因为它们等待 db。其他示例包括聊天或 YouTube 等视频 - 一个堆栈可以同时为更多客户提供服务。基于事件的服务器能够处理数万或数十万个连接的客户端,这样数量的线程会杀死机器。当您确实需要进行一些处理时,它们的效率就会降低。

Indeed event based frameworks are suitable for situations where you have lot of io and less cpu operations, but that defines most of webpages, because they wait for db. Other examples are chats or video like youtube - one stack allows to serve more clients at the same time. Event based servers are able to handle tens or hundreds of thousends connected clients, where such amount of threads would kill machine. They are less efficient when you have really some processing to do.

尘曦 2024-09-11 03:42:16

每个连接没有堆栈。每个处理器核心只有一个堆栈。它实际上一次不能做不止一件事——为什么不等到有事情忙的时候再切换任务,而不是随意来回拉动呢?

No stack per connection. Just one stack per processor core. It's not like it can actually do more than one thing at a time -- why not wait until something is busy to switch tasks, rather than yanking back and forth arbitrarily?

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