Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point/purpose of Ruby EventMachine, Python Twisted, or JavaScript Node.js?

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?

like image 332
CCw Avatar asked May 29 '10 03:05

CCw


People also ask

What Does Node js do?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Is Node js a framework?

js is actually not a framework or a library, but a runtime environment, based on Chrome's V8 JavaScript engine.


2 Answers

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.

like image 87
Glyph Avatar answered Sep 24 '22 02:09

Glyph


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.

like image 27
fiedzia Avatar answered Sep 27 '22 02:09

fiedzia