问题描述
我想知道Web应用程序中的并发实际上是如何工作的.我已经阅读了几篇文章,据我了解,HttpApplication的多个实例将同时工作.现在,我创建了一个简单的Web应用程序来测试并发性,并将以下内容放入global.asax:
I'm wondering how the concurrency in a web application actually works. Ive read several articles and to my understanding multiple instances of HttpApplication would be working at the same time. Now, I created a simple web app to test concurrency and put the following to global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.Write("Request started: " + DateTime.Now);
System.Threading.Thread.Sleep(10000);
Response.Write("<br />");
Response.Write("Request ended: " + DateTime.Now);
Response.End();
}
我期望,如果我几乎同时浏览几个浏览器选项卡中的Web应用程序根目录,它们将同时启动和停止.但是,似乎他们没有.第二个标签开始时间与第一次结束时间相同.然后,我通过在httpmodule或default.aspx page_load中使用相同的代码进行测试,得到了相同的结果.
I was expecting that if I browse to the web app root in several browser tabs at nearly the same time, they would start and stop concurrently. However, it seems that they don't. Second tab start time is same as first time end time. I then tested by having this same code in an httpmodule or default.aspx page_load and got the same result.
这是怎么回事?为什么不并行处理请求?
What is going on here? Why aren't the requests being served parallel?
编辑:我主要将我的理解放在两篇文章上:
I'm placing my understanding mainly to two articles:
http://msdn.microsoft.com/en-us/magazine /cc188942.aspx 说:如果针对同一应用程序的多个请求同时到达,则将使用多个HttpApplication对象."
http://msdn.microsoft.com/en-us/magazine/cc188942.aspx says "If multiple requests targeting the same application arrive simultaneously, multiple HttpApplication objects will be used."
和 http://www.code-magazine. com/article.aspx?quickid = 0511061& page = 5 给出了一个aspx页面的示例,该页面基本上完成了我测试的内容,并带有注释模拟缓慢的请求,以便我们可以并排看到多个请求". Thread.Sleep调用旁边
and http://www.code-magazine.com/article.aspx?quickid=0511061&page=5 has an example for an aspx page doing basically what I tested, with comment "Simulate slow request so we can see multiple requests side by side." next to Thread.Sleep call
可能我完全误解了某些东西……但是呢?
It is possible that I'm completely misunderstanding something... but what?
http://www.code-magazine.com /article.aspx?quickid=0511061&page=5
推荐答案
每个到达的请求都路由到一个单独的HttpApplication对象. HttpApplication对象可以从头开始创建,也可以从池中分配.创建的HttpApplication对象的最大数目受可用线程的最大数目限制.在ASP.NET 1.x中,我认为默认值为20左右.在ASP.NET 2.0中,此限制是动态管理的.
Each request that arrives is routed to a separate HttpApplication object. The HttpApplication object is either created from scratch or allocated from a pool. The maximum number of HttpApplication objects created is limited by the maximum number of threads available. In ASP.NET 1.x I think the default was 20 or so. Under ASP.NET 2.0 this limit is managed dynamically.
在看到应用程序开始崩溃之前,您需要创建足够的请求以耗尽ASP.NET线程池.
You would need create enough requests to exhaust the ASP.NET thread pool before seeing your application start to falter.
代码"文章中的注释的意思不是他的代码使服务器成为瓶颈,而是将其用作一种能够并排查看多个请求(例如不同线程ID)的状态的方法.
What the comment in the Code article means is not that his code is bottlenecking the server, he's using it as a way to be able to see the state multiple requests side by side such as different thread ID's.
这篇关于IIS,Asp.NET管道和并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!