Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can Task.Run be considered good on ASP.NET? #30

Open
paulomorgado opened this issue Nov 13, 2018 · 5 comments
Open

How can Task.Run be considered good on ASP.NET? #30

paulomorgado opened this issue Nov 13, 2018 · 5 comments

Comments

@paulomorgado
Copy link
Contributor

IT might be less bad, but good???

@davidfowl
Copy link
Owner

Generally, there's no performance benefit to using Task.Run in an ASP.NET Core application. You're already on a thread pool thread so you're just moving work off the existing thread pool thread onto another one. Now if you want to compute some data outside the scope of a request, then you may wish to return to the client before that data is computed then you can offload onto another task. That's one scenario where it's useful (fire and forget).

This comment really hits the nail on the head:

https://www.reddit.com/r/csharp/comments/9p26ci/diagnosing_net_core_threadpool_starvation_with/e7yteqs/

100% of the threads your code starts must finish faster than requests are coming in.

@paulomorgado
Copy link
Contributor Author

For fire and forget, I'd choose any option on this page over Task.Run.

@davidfowl
Copy link
Owner

How does that improve the situation? This is all about understanding tradeoffs. The higher is that each requests is conceptually already running inside of a Task.Run (not quite but roughly speaking) so any other work that takes longer than anyone one request could be a bottleneck given the right set of load.

@paulomorgado
Copy link
Contributor Author

The hosting environment can make some guarantees about work it's managing. It can make nothing about Task.Run. Or can it?

@davidfowl
Copy link
Owner

Yes, you can write it such that the background task is aware of the host lifetime, that's about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants