Comments (10)
Accessing .Result would be faster after the task has completed. awaiting the task is more idiomatic but results in a bigger state machine with more states (one for each await point in the method).
Look at the codegen here:
VS
from aspnetcorediagnosticscenarios.
I think another aspect to consider in picking one solution over the other is how do you want to handle exceptions, in particular on how you want to handle the scenario when only some tasks are failing.
from aspnetcorediagnosticscenarios.
Based on feedbacks, conclusion is that first version
var task1 = service.GetAsync1();
var task2 = service.GetAsync2();
await Task.WhenAll(task1, task2);
var result1 = task1.Result;
var result2 = task2.Result;
have quite more performance than second version.
But second version
var task1 = service.GetAsync1();
var task2 = service.GetAsync2();
var result1 = await task1;
var result2 = await task2;
is more idiomatic.
Thanks for precision, it's enough to restart debate with my coworkers.
from aspnetcorediagnosticscenarios.
I've always had this doubt
Regardless of the use of Result
, the first of your codes could execute faster since Task.WhenAll
could execute both Task
s in parallel.
I wonder if in this scenario the first code will be considered good practice ๐ค
Maybe a modified version of the second one
var task1 = service.GetAsync1();
var task2 = service.GetAsync2();
await Task.WhenAll(task1, task2);
var result1 = await task1;
var result2 = await task2;
It would be as fast as first due to Task.WhenAll
and it is also avoiding Result
(await
ing for the completed tasks should be immediate, but a little weird)
from aspnetcorediagnosticscenarios.
So @davidfowl, would would recommend the first version?
var task1 = service.GetAsync1();
var task2 = service.GetAsync2();
await Task.WhenAll(task1, task2);
var result1 = task1.Result;
var result2 = task2.Result;
from aspnetcorediagnosticscenarios.
I've got enough feedback for me. Ticket can be close IMO.
But I see more concern raised by other people.
I let other people close this one, as some questions are pending. If there is no more activities several days, I'll close myself.
Thanks
from aspnetcorediagnosticscenarios.
A third version was introduced by @jjavierdguezas mid conversation and it isn't completely clear to me who is talking about which...
var task1 = service.GetAsync1();
var task2 = service.GetAsync2();
await Task.WhenAll(task1, task2);
var result1 = await task1;
var result2 = await task2;
Does the await Task.WhenAll(task1, task2);
improve or harm performance? @davidfowl uses this in his comparison. What if that line is omitted? Does it simplify the state machine denoted by @davidfowl ?
from aspnetcorediagnosticscenarios.
Wihout any update, I close this issue.
Thanks everyone
from aspnetcorediagnosticscenarios.
@andleer that's even more awaits ๐ so it's not as efficient.
from aspnetcorediagnosticscenarios.
@davidfowl agree that it is even more awaits but you used that scenario in your 2nd sharplap.io / codegen example. I realize it was most likely unintentional but I wonder if the inclusion impacts your comment earlier in this thread. Either way, I will take idiomatic over minimal performance differences. Thanks for all the contributions here!
from aspnetcorediagnosticscenarios.
Related Issues (20)
- IHttpClientFactory Question HOT 2
- Do You Have This Rules as Sonarqube Rules HOT 1
- Feature: Adding contributors section to the README.md file. HOT 2
- Copy Paste error in AsyncGuidance.md HOT 1
- Question about example HOT 2
- AsyncGuidance - Constructors and DI HOT 7
- Make TaskCreationOptions.LongRunning advice more explicit HOT 6
- Analyzer for โPrefer async/await over directly returning Taskโ? HOT 2
- "Always create `TaskCompletionSource<T>` with `TaskCreationOptions.RunContinuationsAsynchronously`" may be detrimentally overcorrecting. HOT 1
- Typo in HttpClientGuidance.md HOT 2
- How about long running work that is done using an async method HOT 5
- Mapping CancellationToken in controller's actions HOT 1
- Use `async/await` in 'A note about WebClient' section
- CPU consumption while using Task.Run vs Background thread HOT 2
- WindowsIdentity.RunImpersonated errors HOT 1
- Possible clarification for long-running work advice HOT 1
- Async void is not always bad HOT 11
- Event handler (async void) with EventArgs processing
- ASP.NET Core does have a SynchronizationContext when using Blazor HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnetcorediagnosticscenarios.