简体   繁体   中英

Do I always need to use async/await?

I wanted to ask you about async/await. Namely, why does it always need to be used? (all my friends say so)

Example 1.

public async Task Boo()
    {
        await WriteInfoIntoFile("file.txt");

        some other logic...
    }

I have a Boo method, inside which I write something to files and then execute some logic. Asynchrony is used here so that the stream does not stop while the information is being written to the file. Everything is logical.

Example 2.

public async Task Bar()
    {
        var n = await GetNAsync(nId);
        _uow.NRepository.Remove(n);
        await _uow.CompleteAsync();
    }

But for the second example, I have a question. Why here asynchronously get the entity, if without its presence it will still be impossible to work further?

why does it always need to be used?

It shouldn't always be used. Ideally (and especially for new code), it should be used for most I/O-based operations.

Why here asynchronously get the entity, if without its presence it will still be impossible to work further?

Asynchronous code is all about freeing up the calling thread. This brings two kinds of benefits, depending on where the code is running.

  1. If the calling thread is a UI thread inside a GUI application, then asynchrony frees up the UI thread to handle user input. In other words, the application is more responsive.
  2. If the calling thread is a server-side thread, eg, an ASP.NET request thread, then asynchrony frees up that thread to handle other user requests. In other words, the server is able to scale further.

Depending on the context, you might or might not get some benefit. In case you call the second function from a desktop application, it allows the UI to stay responsive while the async code is being executed.

Why here asynchronously get the entity, if without its presence it will still be impossible to work further?

You are correct in the sense that this stream of work cannot proceed, but using async versions allows freeing up the thread to do other work:

I like this paragraph from Using Asynchronous Methods in ASP.NET MVC 4 to explain the benefits:

Processing Asynchronous Requests

In a web app that sees a large number of concurrent requests at start-up or has a bursty load (where concurrency increases suddenly), making web service calls asynchronous increases the responsiveness of the app. An asynchronous request takes the same amount of time to process as a synchronous request. If a request makes a web service call that requires two seconds to complete, the request takes two seconds whether it's performed synchronously or asynchronously. However during an asynchronous call, a thread isn't blocked from responding to other requests while it waits for the first request to complete. Therefore, asynchronous requests prevent request queuing and thread pool growth when there are many concurrent requests that invoke long-running operations.

Not sure what you mean by

without its presence it will still be impossible to work further

regarding example 2. As far as I can tell this code gets an entity by id from its repository asynchronously, removes it, then completes the transaction on its Unit of Work . Do you mean why it does not simply remove the entry by id? That would certainly be an improvement, but would still leave you with an asynchronous method as CompleteAsync is obviously asynchronous?

As to your general question, I don't think there is a general concensus to always use async/await.

In your second example there with the async/await keywords you are getting the value of the n variable asynchronously. This might be necessary because the GetNAsync method is likely performing some time-consuming operation, such as querying a database or perhaps you might be calling a webservice downstream, that could block the main thread of execution. By calling the method asynchronously, the rest of the code in the Bar method can continue to run while the query is being performed in the background.

But if in the GetNAsync you are just calling another method locally that is doing some basic CPU bound task then the async is pointless in my view. Aync works well when you are sure you need to wait such as.network calls or I/O bound calls that will definitely add latency to your stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM