What exactly happens when the async method is called without the wait keyword?

I have a web server, and a periodic job combines and sends records (many query logs).

Task.Run(() => { while (true) { try { MergeAndPutRecords(); } catch (Exception ex) { Logger.Error(ex); } } }); 

The MergeAndPutRecords function has code merge entries, and the async function returns task transfer records. (Actaully is Amazon Kinesis Firehose PutRecordBatchAsync .)

Then what happens if I call this function without the await keyword? Does the function execute on a separate chain? It says it is not . Then, what does the task tool return? Here 's an async method without expecting a keyword

  • Runs an asynchronous method for the current thread. Ignores all results (including exceptions).

Then my periodic work and PutRecordBatchAsync are processed at the same time? I know that asynchronous and parallel are different. But the wait keyword is missing and they are in the same thread. Which one will be executed first? I'm confused...

There will be massive records that need to be combined and sent in real time. Therefore, I think that it should be executed at the same time.

+5
source share
2 answers

Then my periodic job and PutRecordBatchAsync are processed at the same time?

Using the Task API, you can guarantee that they will be executed simultaneously (using a thread pool), but you need to understand the difference between concurrency-based IOS-based memory operations.

While it is useful to use Tasks in concurrency memory, an IO call once made does not need a thread at all, since it relies on concurrency hardware if it ever uses a thread, all that it will wait for an IO call to return thereby wasting precious system resources and reducing the scalability of the system.

In the case of concurrency-based IOs, what do you call a remote / network interface API, how does async-wait work here?

Well, a true Async operation will free the thread context, in windows it will use the I / O completion port (queue mechanism) to make an Async call, while the calling thread is used to send other similar calls, it just needs the thread context when returning the I / O call for servicing the response and for this, if it is not a UI call, use ConfigureAwait(false) so that any context stream can be used to deliver the response.

What if you do not use await with async?

A call intended for asynchrony becomes synchronous and immediately affects the scalability of the system, since the threads are now blocked, which is even worse for lengthy I / O operations. You have seen how JavaScript frameworks always make an AJAX (Async) call to the server API, so much more work is possible without blocking browser threads.

In general, to process In-memory, you should create a certain number of Tasks and process them using Task.WaitAll or Parallel.ForEach for the collection, for Async processing, ideally, the recommendation should not have Task.Run anywhere, it should be preferred Async from the entry point, as is possible in the case of MVC, the controllers can be asynchronous. Multiple calls are grouped together using Task.WhenAll representative tasks and then expected. even if you use Task.Run as in your code, then use async lambda to make an asynchronous call

Summary:

It is imperative to use await for asynchronous calls, otherwise they Async keyword is useless in this context, and yes await will wait for the IO call to return before continuing, although no thread is blocked in the process

+5
source

If a method returns Task , it is always best to observe the result of this Task at some point. This may be a declared return value, or it may be an exception. If you want to observe this task before continuing with your method, await is a recommendation.

In this case, you should probably observe the Task result returned by PutRecordBatchAsync . You want to find out if the call caused for some reason, as this probably indicates that your records were not saved!

In the code example below, you will find that you make subsequent calls to MergeAndPutRecords until the previous call completes. Are you sure this is intended?

0
source

Source: https://habr.com/ru/post/1274264/


All Articles