Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Parallel.ForEach use threads from the ASP.NET thread pool? [closed]

I've read that using Task.Run in an ASP.NET web app was a bad idea as it uses another thread from the thread pool and hence prevent this particular thread from being used to serve a request.

Isn't the same situation with Parallel.ForEach? Won't it use multiple threads from the thread pool and hence prevent these particular threads from being used to serve a request ?

like image 257
Anthony Hocquet Avatar asked Jan 04 '23 08:01

Anthony Hocquet


1 Answers

Isn't the same problem with Parallel.ForEach? Won't it use multiple threads from the thread pool and hence prevent these particular threads from being used to serve a request?

Yes, it will. This means that you'd be decreasing the total throughput of your server by having requests using up multiple requests like this. That individual request would complete faster, but it would complete faster at the expense of other requests.

This also assumes that your machine is under a high load. If there isn't a sufficiently high load on the thread pool, then it could afford to dedicate more resources for that request without inhibiting the ability of other requests to be served, and dedicating more resources for that request may well speed up that request.

like image 103
Servy Avatar answered Jan 13 '23 11:01

Servy