Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous vs Asynchronous languages

In last 1 year I was working on Java and flex. While coding flex, most of my code parts went for a toss since its asynchronous. It made me to think about the real advantages and disadvantages of synchronous executing languages vs asynchronously executing ones.

What are the areas where they are stronger compared to other and what are the areas they falls?

like image 960
Umesh Avatar asked Dec 18 '09 19:12

Umesh


People also ask

Is Java synchronous or asynchronous language?

Most server-side languages like Python, C#, Java, and PHP execute code dependently, so one line or an entire block succeeding depends on the success of the one that precedes it. This means they're all synchronous by default.

What are asynchronous languages?

Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.

Is Python synchronous or async?

There are two basic types of methods in the Parallels Python API: synchronous and asynchronous. When a synchronous method is invoked, it completes executing before returning to the caller. An asynchronous method starts a job in the background and returns to the caller immediately.

What's the difference between asynchronous and synchronous learning?

Synchronous learning refers to instructors and students gathering at the same time and (virtual or physical) place and interacting in “real-time”. Asynchronous learning refers to students accessing materials at their own pace and interacting with each other over longer periods.


2 Answers

I've spent most of the last year coding in Silverlight, which means I've spent a good deal of time thinking through (and fighting with) the same issues you're describing.

In brief, as other folks have pointed out, the real strength to the asynchronous model is its ability to create robust systems that interoperate well with the real world. Nobody could realistically use a Silverlight (or Flash) application if the UI thread came to a halt every time it took a few seconds for a web service call to return.

The biggest downside is that it the resulting code is complex and difficult to troubleshoot. Things like error handling are a PITA, but the most annoying stuff I've had to deal with is coordinating responses from multiple asynchronous calls. If, say, you need information from call A before making call B, and you need information from call B before making call C (and so forth), the resulting code looks really unpleasant, and is susceptible to all sorts of weird side-effects. There are techniques for making all this stuff work, and even reasonably clean, but if you're coming from the synchronous world (as I was), it's a significant learning curve. (And it doesn't help that Microsoft pushes events as the way to deal with WCF calls when callbacks, in my opinion, are much cleaner and less susceptible to the sort of weird side-effects I was talking about.)

(And yes, other folks are correct in saying that it's not the language that's asynchronous so much as that particular frameworks require constructing your code in an asynchronous manner -- but I get what you mean.)

Update 2014.09.23 -

I've done a lot more work with a variety of asynchronous frameworks since I wrote the answer above (as has probably everybody else who's done any web coding), and thought I'd add a few additional random notes:

  • If you're using a language like C# or F# that has first-class asynchronous support, a lot of this gets a whole lot easier, at least, once you wrap your head around the weird async/await patterns. Being able to loop easily around asynchronous calls, and wrap the whole thing with a simple try/catch, is amazing if you've ever had to do it the old way.

  • If you're not using a language with first-class async support, start using whatever promise or future or task support that the language does provide (e.g., JQuery's $.Deferred(), or Angular's $q.defer(). Those are a lot cleaner and provide better structure than what you typically get with callbacks.

  • Asynchronous code is critical for writing scalable server-side systems. One of the biggest issues with making a typical web server scale well is that it starts running out of threads, at least, it does if it dedicates a thread to reach incoming request. If that thread stalls, because it's waiting for a long-running synchronous call to finish, it's completely unavailable for helping out with anything else. Much better is to make your web server code async, so that when you're waiting on a DB call to return, that thread can go service half a dozen other requests while the DB is going and doing whatever DB's do. At this point, for highly scalable systems, async is the only game in town. (Just ask any Node aficionado.)

like image 136
Ken Smith Avatar answered Oct 12 '22 10:10

Ken Smith


(Leaving aside the semantics level discussion i.e. "sync/async language")

A "framework" built on a "language" (whatever it is) should be able to handle such cases (sync/async programme flow) in order to be useful (read: $ wise).

Asynchronous idioms are appropriate at every scale.

At large scale, asynchronous techniques help build reliable systems because the real world is anyhow asynchronous in nature. In other words, one needs to think in "asynchronous thoughts" to cope with real-life situations such as failures, losses, delays etc.

Even on a smaller scale (e.g. GUI application), events (such as "mouse clicks") tend to be "asynchronous". Of course they are "serialized" at some point (in order to be processable by an application running some software) but it doesn't change the fact that events (probably) occurred "asynchronously" with regards to the flow of the programme in question.

like image 36
jldupont Avatar answered Oct 12 '22 09:10

jldupont