R
R
Rouslan9432021-01-19 09:56:35
Asynchronous programming
Rouslan943, 2021-01-19 09:56:35

Is asynchrony a delay?

All tutorials talk about asynchrony in the context of deferred operations. Is it really just a delay? I thought it was parallel execution of different tasks in one thread. Kind of multitasking.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
freeExec, 2021-01-19
​​@Rouslan943

In general, yes. Imagine you have a code

var imageFromInternet = GetImageByUrl("http://example.com/big-image.bmp");
picture.image = imageFromInternet;

There is an option when GetImageByUrl slows down your code until the picture from the Internet is loaded, and this can be up to half an hour. And if you need to say 10 such pictures, it will take more than a day. And everything is frozen, you can't even draw a progress bar, because your code is being executed somewhere inside GetImageByUrl.
And there is an option when the operation handle is returned to you and your code immediately continues further. Here it is deferred, in fact, the task will be completed sometime later, and not now. And you get the opportunity to put all 10 pictures on the race at the same time. And then sit and wait when they are all downloaded. And only then do something with them.
In fact, all operations with devices are asynchronous inside, otherwise everything would get up with a stake until it waited for a response.

A
Alexander Talalaev, 2021-01-19
@neuotq

Why consider primarily in the context of pending operations?
The problem is that, by default, scripts are executed sequentially and synchronously. Thus, any operation that takes time will stop all program execution. And very often there are other tasks that, in general, can be performed independently. To do this, in many languages ​​/ environments, tools have been created for asynchronous execution of part of the operations. A special mechanism adds the ability to indicate that a certain operation is asynchronous and there is no need to wait for its completion, you can continue to execute the program further. At the same time, with the help of other operators, we can specify other operations that will be performed at the moment when the pending one is completed.
Let's say you make and display a program interface with a list of participants with various complex data. So, you can make a request for the data itself and the list asynchronous and immediately show and draw the entire interface (menus, buttons, table headers) to the user, and mark the table itself at that moment as "Loading", and then after receiving the data from the database, the function waiting for the response of our asynchronous request will already draw only the rest of the interface, the data itself. In this way, we will improve the user experience, because otherwise the user looks at a blank screen until we receive and prepare all the data to show everything at once.
Well, yes, often asynchrony also allows you to conveniently parallelize tasks, increasing the overall performance of the system as a whole.

T
tundramani, 2021-01-19
@tundramani

synchronous = serial
asynchronous = delayed

D
d-stream, 2021-01-19
@d-stream

Probably quite simply: "we do not wait." That is, "go do it, at the end you will report [report every half an hour]" and then we go about our business.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question