views:

719

answers:

3

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?

+1  A: 

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?

Aredridel
I buy that waiting for other things takes up a lot of time, but what about a situation where a particular page has a lot of processing going on and takes ~500ms to complete. That means that every other request between its start and end is going to have to wait up to 1/2 second before it even starts. I'm thinking a script that does a lot of number crunching on things that are not retrievable in an already-compiled fashion, i.e. lots of CPU, little I/O.
CCw
@CCw: No it wouldn't, at least with node.js you set up an event for that process and get a callback when it's done (in short words), while the rest of the script keeps working in async and serving requests. What you're describing is the opposite, the 'sync' method.
stagas
Indeed, something that does 500ms of computation per page is going to need a lot of CPUs to get concurrency -- or should be sliced off as a background task, with an event for its completion. Most web systems balk with many of this sort of thing running -- so many stacks hanging around, threads being switched and nobody getting much done. Asynchronous programming forces one to isolate those badly behaved bits of code, and think about the system as a whole.
Aredridel
A: 

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.

fiedzia
+3  A: 

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.

Glyph