简体   繁体   English

.NET Web应用程序本机使用了多少个线程?

[英]How many threads does a .NET web application natively use?

This has been bugging me for a while. 这一直困扰着我。 Assuming that I am not using any explicit form of task parallelism (eg Paralell.ForEach ), how many threads are natively used when I deploy a web application to a Windows Server? 假设我没有使用任何显式形式的任务并行(例如Paralell.ForEach ),当我将Web应用程序部署到Windows Server时,本机使用了多少个线程?

In other words, if the web server has an eight-core processor, does the application only use one of those cores if I am not explicitly telling it to use more? 换句话说,如果Web服务器具有八核处理器,那么如果我没有明确告诉它使用更多内核,那么应用程序是否只使用其中一个内核?

I'll bet I have missed something simple here, so flame on--but I still want to know the answer. 我敢打赌,我在这里错过了一些简单的东西,所以火上浇油 - 但我还是想知道答案。 Thanks. 谢谢。

First, you have to consider that a web server is, by its nature, a multi-threaded system because it has to be able to respond to multiple requests at the same time (there are ways to do this in a single threaded environment, but it's not very efficient, and IIS does not support it). 首先,您必须考虑Web服务器本质上是一个多线程系统,因为它必须能够同时响应多个请求(有一些方法可以在单线程环境中执行此操作,但是它效率不高,IIS不支持它。 If two users access the site at the same time, two separate threads are servicing each request in most cases. 如果两个用户同时访问该站点,则在大多数情况下,两个单独的线程正在为每个请求提供服务。

Once you get into scalability issues, and async support, then a thread can actually service multiple requests (or rather, a request can be put on a queue and the IIS worker threads can be reused to process other requests). 一旦遇到可伸缩性问题和异步支持,那么一个线程实际上可以为多个请求提供服务(或者更确切地说,可以将请求放在队列中,并且可以重用IIS工作线程来处理其他请求)。

So you are looking at this from the aspect of a single user. 所以你从单个用户的角度来看这个。 And that single user will likely run only a single thread of activity during that web request (ignoring async, io completion ports, etc..). 并且该单个用户可能在该Web请求期间仅运行单个活动线程(忽略异步,完成端口等等)。 ie you can think of a single web request as being equal to a single thread in the general sense (but there are so many things that can spin off other threads, that this isn't something that you can really count on). 也就是说,你可以认为单个Web请求在一般意义上等于单个线程(但是有很多东西可以脱离其他线程,这不是你真正可以依赖的东西)。

When you use something like Parallel.ForEach it's so that your single users request can now execute things in multiple threads to improve efficiency. 当您使用Parallel.ForEach之类的东西时,您的单个用户请求现在可以在多个线程中执行操作以提高效率。

An IIS web site has a worker process associated with it. IIS网站具有与之关联的工作进程。 That worker process can be configured and tuned to control how many threads it uses. 可以配置和调整该工作进程以控制它使用的线程数。 But just remember, that your web site is running under the control of IIS, not as it's own application. 但请记住,您的网站是在IIS的控制下运行的,而不是它自己的应用程序。

The topic of threads can get really confusing as you move along .NET versions. 随着.NET版本的发展,线程主题会变得非常混乱。

But to over-simplify, it depends on your processModel configuration for ASP.NET. 但要过度简化,这取决于ASP.NET的processModel配置。 (machine.config) autoConfig=true/false etc. (machine.config) autoConfig = true / false等。

at a bare minimum, in .NET 4.0, the min/max WorkerThreads and min/max IOCompletionThreads determine the amount of threads in play. 在.NET 4.0中,最小/最大WorkerThreads和最小/最大IOCompletionThreads确定了正在播放的线程数量。

eg If the minWorkerThreads = 1 (default value), then the total minThreads = 1 * no:of cores .. same with maxWorkerThreads . 例如,如果minWorkerThreads = 1(默认值),那么整个minThreads = 1 * no:of cores ..与maxWorkerThreads相同。

Based on the load, the worker process can ramp up the threads needed to service the requests, using an undisclosed algorith.. minWorkerThreads to maxWorkerThreads. 基于负载,工作进程可以使用未公开的算法来增加服务请求所需的线程.. minWorkerThreads to maxWorkerThreads。 (of course based on availability of ThreadPool threads etc.) (当然基于ThreadPool线程的可用性等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM