Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I wrap slow calls with Task.Run?

I have this ASP MVC call which is aync because of an async call it needs to make. I noticed there are a couple of slow sync calls (e.g. db access). The method needs to have all returned data available in order to proceed.

I thought of wrapping the sync calls with Task.Run and await for all of them.

Does it make sense to wrap the slow sync calls? What if there was only sync calls?

like image 331
Igor Gatis Avatar asked Dec 26 '22 07:12

Igor Gatis


1 Answers

Don't use Task.Run to parallelize work in the server-side code, unless the number of client requests you expect to serve concurrently is really low. Otherwise, you might speed up the processing of an individual request, but you'll hurt scalability of your web app for when there are many users.

like image 121
noseratio Avatar answered Jan 19 '23 04:01

noseratio