by viraptor · 16y ago · 0 comments
> Multithreaded server coding is more intuitive - You simply follow the flow of whats going to happen to one client
Is it really? I'd agree if we were talking about something like server spawning a scripting language probably, but in other cases? Most network endpoints are described as state machines. They're not supposed to "flow" - they're responding to input and changing their state. I'd claim that if you implement a protocol with more than 5 states as a properly abstracted state machine, it would be more intuitive than anything that's supposed to emulate "flow". And that's more natural in asynchronous design.
wait_for_response(request, function(response) {
do_something_with_response(response)
...
})
This becomes even more clear in Ruby syntax, where you don't have to nest the reply handler into the wait_xxx function: wait_for_response(request) { |response|
do_something_with_response(response)
...
}
Using monads, you can even abstract the difference away completely. That way, the code looks like ordinary imperative stuff: response <- wait_for_response(request);
do_something_with_response(response)
If your language provides continuations, you can even handle multiple responses that way. Paul Graham did that in Viaweb (http://lib.store.yahoo.net/lib/paulgraham/bbnexcerpts.txt, section "Closures Simulate Subroutines").tl;dr: The asynchronous I/O is only hard to use in languages which lack essential features. In modern languages you can write "intuitive" code that works for both cases, and you are free to switch from multithreaded sync I/O to asynchronous I/O as you like.
The typical web app falls in to the former category: validate input, read from cache, maybe read from database, maybe write to database, generate response.
while (read(...)) {
...
}
instead of: if (read(...) == EWOULDBLOCK) {
yield();
} else {
manage buffer
}Only a tiny bit of your code ever touches send/recv. It never gets EWOULDBLOCK (it's only running because select told it to!). Most of your code deals with entire requests; the state it's saving is largely identical to what a sychronous server deals with; the overhead is in having to hot-potato it through multiple functions.
(Which in turn implies that all the other libraries you are using --at least ones that need to communicate-- need to have pretty deep integration with your "good library," or you're going to be writing that integration yourself.)
(Not that threading is immune here... you need to be careful choosing libraries such that they are re-entrant, etc. But threading has held more mindshare for much longer, and most OSes share a roughly similar model, so they have a large head start on this front.)