2 ms·
If we look at Django or Ruby on Rails, arguably the two most promising new web application frameworks to emerge in the past few years, both of them are written
by patio11 17y ago
If we look at Django or Ruby on Rails, arguably the two most promising new web application frameworks to emerge in the past few years, both of them are written in such a way that synchronous programming is assumed.
Well, yes, but really no. Within a single process, everything is synchronous. Deployments in the real world are asynchronous via use of multiple processes, shared nothing architecture, and a few forms of interprocess communication. (The database is pretty common, though I guess that means shared-something. Let MySQL programmers deal with the semaphores. I can't even spell semaphore and I've got customers to satisfy.)
In real deployment scenarios, you can achieve I-can't-believe-its-not-asynchronous-processing by layering the same programming techniques and battle-tested, well-supported libraries you already use.
One hacky solution which works well in practice: give the browser as much as you can, have it AJAX poll for anything you don't have ready. In practice, since page rendering is Very Effing Slow relative to many other things you want to do, this increases user perception of speed considerably.
For example, hypothetically say you want to do a word count on 47 blogs and display that to the user (I don't know why you would want to do this, but it looks like his example). No problem. Check your cache for any of the blogs you've already calculated and spit that right into the response HTML. Have the browser poll you for the rest of them via AJAX. Meanwhile, you kick off the HTTP access and subsequent word count operation to any arbitrary number of worker processes, and after you're done with them, write them to the cache (or db, etc).
- aaronblohowiak 17y agoAt the point where you "kick off the http access and subsequent word count operation", you are performing an asynchronous operation. The fact that you are using os processes to handle it instead of language primitives does not change the synchrony.
- gridspy 17y agoBut you are using nice chunky grained blocks of asynchronous-ity (which makes it easier) rather than spreading it evenly throughout your program (often quite hard).
- aaronblohowiak 17y agoIt Depends. I wasn't trying to lobby for or against different granularities of asynchrony or where you chose to branch the execution path, but was merely trying to point out that asynchrony is still involved. One of the more confusing things that I've experienced recently is that some programmers get a little fuzzy when you ask if an HTTP request is synchronous. That is, is the body of an HTTP response considered synchronous communication with the client. In my view of the system, HTTP (sans keep-alive) supports a single synchronous message and response, whereas some people try to assert that the response happens after the completion of the request is sent, so the communication is actually asynchronous. From that point of view, I can see how the terminology is vague. However, if we are talking about program control flow and say that blocking on a function call is synchronous and various other forms of deferred evaluation are asynchronous, then the HTTP lifecycle starts to fit the "synchronous" model more closely (note that I'm not talking about http client libraries here.) Anyway, this does lead into one of my favorite interview questions: "Is JavaScript multithreaded?" Many people get hung up on AJAX and setTimeout... but these are the same people that don't really get the tech at a fundamental level.
- gridspy 17y agoOn an initial page load, the user tends to be blocking on the page response - the user is forced to treat it as a synchronous operation, though they can task switch to other pages. On the server side, the merit of asynchronous processing depends on the goal. I'm quite happy to spin up multiple processes and have each process synchronous process a single request on the server - my time is too valuable to debug the alternatives at the moment. I'm quite happy to admit that my HTTP requests are processed synchronously by Apache, mod_python and django. Even the out-of-band requests from ajax also tend to be synchronously processed. On my power dashboard, I handle ongoing comms using Twisted and totally asynchronously - pushing data to the user through Orbited. This gives our system an extremely fast response rate. Event loop driven code is the key to decent firmware performance. It is how we ensured that the responsiveness our our hardware solutions in the past (such as the Kiddie Kart) was very good.