Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between: Asynchronous, Non-Blocking, Event-Base architectures?

  1. What's the difference between:

    • Asynchronous,
    • Non-Blocking, and
    • Event-base architectures?
  2. Can something be both asynchronous and non-blocking (and event-based)?

  3. What's most important in programming, to have something: asynchronous, non-blocking and/or event-base (or all 3)?

If you could provide examples, that would be great.

This question is being asked because I was reading this great StackOverflow article on a similar topic but it doesn't answer my questions above.

like image 361
nickb Avatar asked Oct 28 '11 15:10

nickb


People also ask

What is the difference between asynchronous and non-blocking?

Non-Blocking: It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line.

What's the difference between blocking and non-blocking functions?

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution.

Are blocking events asynchronous?

Blocking methods execute synchronously and non-blocking methods execute asynchronously.

What is the difference between blocking calls and non-blocking calls?

Blocking call waits for the I/O operation to complete before returning. Its results are returned synchronously. Nothing else in that process takes place during the waiting. In contrast, non-blocking call returns immediately without results and uses alternate means to check for completion.


1 Answers

Asynchronous Asynchronous literally means not synchronous. Email is asynchronous. You send a mail, you don't expect to get a response NOW. But it is not non-blocking. Essentially what it means is an architecture where "components" send messages to each other without expecting a response immediately. HTTP requests are synchronous. Send a request and get a response.

Non-Blocking This term is mostly used with IO. What this means is that when you make a system call, it will return immediately with whatever result it has without putting your thread to sleep (with high probability). For example non-blocking read/write calls return with whatever they can do and expect caller to execute the call again. try_lock for example is non-blocking call. It will lock only if lock can be acquired. Usual semantics for systems calls is blocking. read will wait until it has some data and put calling thread to sleep.

Event-base This term comes from libevent. non-blocking read/write calls in themselves are useless because they don't tell you "when" should you call them back (retry). select/epoll/IOCompletionPort etc are different mechanisms for finding out from OS "when" these calls are expected to return "interesting" data. libevent and other such libraries provide wrappers over these event monitoring facilities provided by various OSes and give a consistent API to work with which runs across operating systems. Non-blocking IO goes hand in hand with Event-base.

I think these terms overlap. For example HTTP protocol is synchronous but HTTP implementation using non-blocking IO can be asynchronous. Again a non-blocking API call like read/write/try_lock is synchronous (it immediately gives a response) but "data handling" is asynchronous.

like image 71
Rohit Karlupia Avatar answered Sep 30 '22 02:09

Rohit Karlupia