Home computickets What is asynchronous in programming?

What is asynchronous in programming? [Duplicate]




I do not understand why the approach is called asynchronous, if in essence, the execution of these functions is simply transferred to the end of the synchronous queue (Event Loop). The same, if they are simply called the last? I thought it was when the tasks in one thread are performed in parallel.

Answer 1, Authority 100%

It is worth explained by the example. In one of the handlers, we sent a request to the database. The database processes 5 seconds.

app.get ('/ SyncRequestFortest', Function (REQ, RES) {// Server can accept and process one request in 5 seconds.
  Let user = yourdb.users.findsync ({_ ID: REQ.DATA.ID}) // We have a lot of database data, so the database processes such a request for 5 seconds.
  REQ.SEND (User)
App.Get ('/ Other', Function (REQ, RES) {// Server cannot process this request while it is busy synchronous computing.

In this example, the program will wait for a synchronous request for 5 seconds until the database returns the answer. These 5 seconds it cannot process other requests, which is unacceptable in production.

If the query is asynchronously, then during the response to the response from the database, the server will be able to process other requests:

app.get ('/ asyncrequestfortest', async function (REQ, RES) {// Server can take hundreds of such requests to give me a sec.
  Let user = await yourdb.users.Findasync ({_ ID: REQ.DATA.ID}) // We have a lot of database data, so the database processes such a request for 5 seconds.
  REQ.SEND (User)
App.Get ('/ Other', FUNCTION (REQ, RES) {// Server can process this request, while somewhere outside the main flow of the application is performed asynchronous tasks.

Usually an asynchronous approach is used to request to third-party programs (Fetch-query in the browser, reading and writing files, database requests). Because these requests usually last relatively long and do not load resources (all that makes the application is waiting), and to suspend the operation of the entire application (for example, server), while you read the file for processing one of the requests, is unacceptable.

Here This article in English describes the difference between synchronous , asynchronous execution, and also concerns multithreading.

Here article on Habré with an explanation of the difference between these approaches.

Here is Question in Russian stackoverflow where a lot good examples on the difference between multithreading, asynchronism, synchronicity and competitiveness.

Duplicate here:

Multi-threaded programming implies that application code
Performed in different streams. For example, there is a main stream of UI, and
Several workflows that perform heavy calculations,
The results of which are then displayed on the UI.

Asynchronous programming implies the initiation of some
operation, about the end of which the main stream recognizes after some
time. This is usually used to work with I / O system:
disks, network, etc. At the same time, if it’s all done correctly, no
There is no stream. Also often under the expression “Run asynchronous”
imply that the implementation of some code will be made not in
the current stream, and in the neighboring, with the current flow will not be
blocked. But my opinion is not entirely correct.

Parallel programming implies the partition of one task on
independent subtasks that can be calculated in parallel and then
Combine results. One example is map-reduce. This is private
Case of multi-threaded programming.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions