开发者

.NET 4 Parallel namespace in a server environment - pros and cons?

开发者 https://www.devze.com 2023-02-26 15:28 出处:网络
I have 开发者_Python百科been playing with the Parallel namespace of .NET4 on client apps a little. Seems to do a pretty decent job and really speeds up loops easily without worrying about threads.

I have 开发者_Python百科been playing with the Parallel namespace of .NET4 on client apps a little. Seems to do a pretty decent job and really speeds up loops easily without worrying about threads. However, what is the recommendation in a server environment, on a website powered by ASP.NET? If I imagine a server with four CPU cores and I use some code based on Parallel namespace's methods, I can loop whatever list I have pretty fast. At the same time I will probably block all four CPU cores. If instead I use one thread only to do the looping, other users may access the same page of my website and get a response - just slower. So how do Parallel namespace, ASP.NET and IIS play together? Is it somehow managed?


Personally, my biggest concern was sessions (in ORM tools) not being thread safe thus any thread would require its own session. So it is one major issue to take into account while using ORM tools (if any, i.e. entity framework) and auto-parallelism.

Also straight from MSDN: The .NET Framework Parallel Extensions allow you to nest parallel loops. In this situation, the run-time environment coordinates the use of processor resources. You'll find that with nested parallel loops, each loop uses fewer threads than in a non-nested loop.

A related problem is the handling of parallel loops in server applications. The Parallel class attempts to deal with multiple AppDomains in server applications in exactly the same manner that nested loops are handled. If the server application is already using all the available thread pool threads to process other ASP.NET requests, a parallel loop will only run on the thread that invoked it. If the workload decreases and other threads become available and there's no other ASP.NET work to be processed, the loop will start using additional threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号