-
Notifications
You must be signed in to change notification settings - Fork 763
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
Async code inside a long running background thread #48
Comments
It's fine to use do background async work the trick is to understand that if you do async work, you're not blocking a running thread. The thread pool is very likely still being used to use resume the async operation. As long as you're ok with that then keep on doing what you're doing (without using .Result of course). What does your code look like today? |
Today our Startup.cs Configure methods start "and forget" about 10 tasks using:
where MethodLoadingTableAIntoMemoryCache, MethodLoadingTableBIntoMemoryCache,.. methods are async. |
You should definately read great 3-part series from Andrew Lock about how you can run async code on startup: |
Hi, @davidfowl I have seen this code, where you suggest spawning a new thread manually and rembemered this from Concurrency in C# Cookbook from Stephen Cleary. Is this one of those exceptions :) ? |
Arrived to this great repository after watching your great talk on scalability on youtube.
The situation here:
https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskrun-for-long-running-work-that-blocks-the-thread
is relevant for us (we have ~10 such workers which keep synchronizing some database tables, each worker for a different table, into memory cache so that when http requests come we can handle them quickly using the data from the memory cache instead of querying the database) however we run some async calls which don't have alternative sync calls from that background worker and the example shown is sync so it is unclear how to correctly start async code in that new background thread without going back to using .Wait or .Result?
Maybe in such cases we need to use Task.Factory.StartNew with TaskCreationOptions.LongRunning which allows to smoothly run async task? and if so can you please provide an example of how to correctly do it because I saw that you wrote that its error prone if to pass wrong parameters
Thanks!
The text was updated successfully, but these errors were encountered: