Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async await best practices

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

  1. is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

  2. in an application such as winforms, while UI runs on it's thread, using async/await on an operation such as a button click is fairly trivial, but what about if I would like to enforce asynchronously throughout an entire console application, or even a windows service. what is the best practice to initially kick off that first await task, would that be Task.Run (() => ... )?

I hope I am making sense in my 2nd question. I want to make the most of async and utilize it to it's full extent, but just need to understand how to kick off the initial asynchronous operation before it bubbles down to all other asynchronous functions.

apologies for not using the proper code blocks I am on the train using my smartphone.

like image 229
Sash Avatar asked Feb 18 '13 05:02

Sash


People also ask

Which is better async await or then?

Async/await and then() are very similar. The difference is that in an async function, JavaScript will pause the function execution until the promise settles. With then() , the rest of the function will continue to execute but JavaScript won't execute the . then() callback until the promise settles.

Is async await better than promise?

Using Async/Await makes it easier to read and understand the flow of the program as compared to promise chains.

Does async await improve performance?

The main benefits of asynchronous programming using async / await include the following: Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution.

Should I use async await?

Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.


2 Answers

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

I have an intro to async/await blog post that goes into more detail than most intros and also introduces several best practices.

is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

You want to avoid tight loops. So the while (condition) GetDataIfPresent(); is going to consume a lot of CPU.

Alternatively, you could use an async method that returned null (or whatever) if stopProcessingMessages is true. In this case, your code would be while (true), and a more TAP-like solution would be to use CancellationSource instead of a flag.

Also take a look at TPL Dataflow; it sounds like it may be useful for your kind of situation.

console application, or even a windows service. what is the best practice to initially kick off that first await task

For console apps, you could Wait on the top-level task. This is an acceptable exception to the usual guideline (which is to await instead of Wait). Waiting will burn a thread for the duration of the console app, but that's usually not important enough to warrant a more complex solution. If you do want to install a single-threaded context for your console app, you could use AsyncContext.Run from my AsyncEx library.

For Win32 services, you usually do need to start your own thread. You can use Task.Run for this (if you want a multithreaded context), or AsyncContextThread from AsyncEx (if you want a single-threaded context).

like image 95
Stephen Cleary Avatar answered Nov 08 '22 19:11

Stephen Cleary


Good morning,

I would rather use a regular task with the TaskCreationOption set to 'LongRunning' in your first scenario than the async/await pattern. This way your whole while block would be executed in one long running task. When using await inside each while loop you would start a new task with every loop - would work, but it's maybe not so optimal ;-)

Regarding your second question, I'm sorry but I don't get your point.

Hope this helps.

like image 27
robert.oh. Avatar answered Nov 08 '22 21:11

robert.oh.