Easy to Understand Definition of "Asynchronous Event"

Easy to understand definition of asynchronous event?

Non programming example:

Synchronous
You want a pizza for dinner and you are out of the frozen kind. So you have to stop playing WOW which upsets your guild. You go to the kitchen, make the dough, cover it with sauce, add the cheese, and smother it your favorite bacon topping. You just spent 20 minutes of your time making the pizza with another 10 minutes in the oven. The timer beeps and you pull the hot pie out. You can sit back down in front of your computer, eat the pizza, and continue with your raid.

Asynchronous
You want a pizza for dinner while playing WOW. You open up a browser window on your 5th monitor. You load up the Pizza website and order your extra cheesy bacon pizza with a side of bacon grease garlic sauce. You go back to your raid and after 20 minutes the door bell rings. You get the pizza. You sit back down in front of your computer, eat the pizza, and continue with your raid.

So what is the difference? One way you waste 20-30 minutes of precious WOW time, the other way you waste $20 plus tip.

Asynchronous vs synchronous execution. What is the difference?

When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

In the context of operating systems, this corresponds to executing a process or task on a "thread." A thread is a series of commands (a block of code) that exist as a unit of work. The operating system runs a given thread on a processor core. However, a processor core can only execute a single thread at once. It has no concept of running multiple threads simultaneously. The operating system can provide the illusion of running multiple threads at once by running each thread for a small slice of time (such as 1ms), and continuously switching between threads.

Now, if you introduce multiple processor cores into the mix, then threads CAN execute at the same time. The operating system can allocate time to one thread on the first processor core, then allocate the same block of time to another thread on a different processor core. All of this is about allowing the operating system to manage the completion of your task while you can go on in your code and do other things.

Asynchronous programming is a complicated topic because of the semantics of how things tie together when you can do them at the same time. There are numerous articles and books on the subject; have a look!

Can someone explain to me what is an event-driven, asynchronous, non-blocking I/O

Imagine you read from a socket with this pseudo code

void processIO(socket)
{
data = socket.read();
doSomething(data);
}

The read method is used in a blocking mode. That means it does not continue until the data is read. The thread, this is running on is blocked and does not continue until the data is read. doSomething is only called once the data is read. If you did this on a main thread of an app, it would probably not be able to update its UI and would behave like frozen until the data is received.



async void processIO(socket)
{
data = await socket.readAsync();
doSomething(data);
}

This function is asynchronous and it itself calls an asynchronous readAsync() method.
In this way, the thread this runs on is not blocked. It is interrupted at the await statement, and is available for other things in your app to run. Once the data is read, it resumes after the await statement and continues with doing something with the data.
This is one way of doing non-blockig data processing however it is not event driven.



void setupRead(socket)
{
socket.on_data(do_something);
}

void do_something(data)
{
// process data
}

void main()
{
socket = new Socket(111)
setupRead(socket)
while (true) {
processEvents()
}
}

This last example demonstrates event driven IO. You register a callback on some resource to be called when some data arrives. In the mean time your code may do other things or do nothing. This is a non-blocking, asynchronous and event driven approach. The UI get's refreshed and the app may do whatever it needs to.

Event Driven, means you setup event callbacks and wait for the events to happen.
Asynchronous means, you do other stuff while waiting such as refreshing UI, processing user input or read from write from other resources.

Non-blocking means the thread that started the listening, is not blocked until an event arrives, it does whatever else there is to do. Such as handle other events in the mean time.

Event Based == Asynchronous?

No it doesn't imply that the events are asynchronous.

In a event driven single threaded system, you can fire events, but they are all processed serially. They may yield as part of their processing, but nothing happens concurrently, if they yield, they stop processing and have to wait until they are messaged again to start processing again.

Examples of this are Swing ( Java ), Twisted ( Python ), Node.js ( JavaScript ), EventMachine ( Ruby )

All of these examples are event driven message loops, but they are all single threaded, every event will block all subsequent events on that same thread.

In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

So just because something is event driven doesn't make it asynchronous, and just because something is asynchronous doesn't make it event driven either; much less concurrent.

Why are Asynchronous processes not called Synchronous?

It means that the two threads are not running in sync, that is, they are not both running on the same timeline.

I think it's a case of computer scientists being too clever about their use of words.

Synchronisation, in this context, would suggest that both threads start and end at the same time. Asynchrony in this sense, means both threads are free to start, execute and end as they require.

Asynchronous and Synchronous Terms

Indeed, it's one of these cases, where original meaning of the word was subverted and means something different than in popular usage.

'Synchronisation' in telecommunication means that receiver signals whenever it is ready to receive messages, and only after this signal the transmitter will start transmitting. When the transmitter is done with the message, it will signal it has finished, so that receiver can now process the received message and do whatever it is supposed to be doing next.

This is of course a simplification and a very broad one, but it should give you the feeling of from where the meaning of '(a)synchronous' comes in JS.

So synchronous request in JS is actually synchronised with the main flow of the program. The program sends request to server ('I'm ready to receive') and waits for message. Message from the server will have a well defined end ('the message ends here - do your job'). When it is received, JS knows it can continue with execution of the program..

What's the difference between event-driven and asynchronous? Between epoll and AIO?

Events is one of the paradigms to achieve asynchronous execution.
But not all asynchronous systems use events. That is about semantic meaning of these two - one is super-entity of another.

epoll and aio use different metaphors:

epoll is a blocking operation (epoll_wait()) - you block the thread until some event happens and then you dispatch the event to different procedures/functions/branches in your code.

In AIO, you pass the address of your callback function (completion routine) to the system and the system calls your function when something happens.

Problem with AIO is that your callback function code runs on the system thread and so on top of the system stack. A few problems with that as you can imagine.

Looking for an explanation of the 'Asynchronous' word in .Net?

'Asynchronous' describes a type of execution flow.

Synchronous instructions execute linearly and prevent subsequent instructions from executing until complete (that is, they block). So given the following synchronous code:

DoOneThing();
DoAnotherThing();

DoAnotherThing doesn't execute until DoOneThing is finished.

Asynchronous instructions differ in that you don't know (or sometimes even care) when they start or finish executing. In a case like this:

DoOneAsynchronousThing();
DoAnotherThing();

The first statement initiates the asynchronous operation, then does another thing immediately before the first operation is completed (or perhaps even started).

There are many different mechanisms for providing asynchronous execution: the most common ones (at least in the .NET world) are probably the ThreadPool (for in-process asynchronous execution) and Microsoft Message Queue (for inter-process asynchronous execution). For a .NET-specific introduction, you might start with this MSDN topic, "Including Asynchronous Calls".

So asynchronous delegates, methods, and events all run (and complete) at indeterminate times and do not block the main thread of execution.



Related Topics



Leave a reply



Submit