[英]ASP.NET async/await part 2
I have a variation of the benefits-of-async/await-on-ASP.NET from this question . 我从这个问题中得到了 async / await-on-ASP.NET的各种好处。
My understanding is that asynchrony is not the same thing as parallelism. 我的理解是,异步与并行性不是一回事。 So, on a web server, I wonder about how much benefit async/await brings to ASP.NET pages.
因此,在Web服务器上,我想知道async / await给ASP.NET页面带来了多少好处。
Isn't IIS+ASP.NET already really good at allocating threads for requests, and if onen page is busy waiting for a resource the server will just switch to processing another request that has work to do? 是不是IIS + ASP.NET已经非常擅长为请求分配线程,如果onen页面忙于等待资源,服务器将只切换到处理另一个有工作要求的请求?
There are a limited number of threads in the pool for ASP.NET to use - does async use them any more effectively? ASP.NET中可以使用有限数量的线程供ASP.NET使用 - 异步使用它们是否更有效?
As Mr. Skeet pointed out in answering the question above, we're not talking about blocking a UI thread. 正如Skeet先生在回答上述问题时指出的那样,我们不是在谈论阻止UI线程。 We're already multi-threaded and the web response can't be completed until all the request's tasks are done, async or not, right?
我们已经是多线程的,并且在完成所有请求的任务之前无法完成Web响应,异步与否,对吗?
I guess what it boils down to is this: 我猜它归结为:
Is there any benefit to an async read of a resource (say a file or DB request) in an ASP.NET page vs. blocking on it? 在ASP.NET页面中对资源(例如文件或数据库请求)进行异步读取与阻止它有什么好处?
if one page is busy waiting for a resource the server will just switch to processing another request that has work to do?
如果一个页面忙于等待资源,服务器将只切换到处理另一个有工作要求的请求?
I don't think so. 我不这么认为。 I would be very surprised if this were the case.
如果是这样的话,我会非常惊讶。 It's theoretically possible, but very complex.
这在理论上是可行的,但非常复杂。
There are a limited number of threads in the pool for ASP.NET to use - does async use them any more effectively?
ASP.NET中可以使用有限数量的线程供ASP.NET使用 - 异步使用它们是否更有效?
Yes, because when you await
something, the thread for that request is immediately returned to the pool. 是的,因为
await
某事时,该请求的线程会立即返回到池中。
We're already multi-threaded and the web response can't be completed until all the request's tasks are done, async or not, right?
我们已经是多线程的,并且在完成所有请求的任务之前无法完成Web响应,异步与否,对吗?
That is correct. 那是正确的。
async
in a server scenario is all about removing pressure on the thread pool. 服务器方案中的
async
就是消除线程池上的压力。
Is there any benefit to an async read of a resource (say a file or DB request) in an ASP.NET page vs. blocking on it?
在ASP.NET页面中对资源(例如文件或数据库请求)进行异步读取与阻止它有什么好处?
Absolutely! 绝对!
If you block on a file/service call/db request, then that thread is used for the duration of that operation. 如果阻止文件/服务调用/ db请求,则该线程将用于该操作的持续时间。 If you
await
a file/service call/db request, then that thread is immediately returned to the thread pool. 如果
await
文件/服务调用/ db请求,则该线程立即返回到线程池。
One (really cool!) result of this is that you can have a request in progress, and while it's (a)waiting some operation, there are no threads servicing that request! 一个(真的很酷!)结果是你可以有一个正在进行的请求,虽然它是(a)等待一些操作,但没有线程服务该请求! Zero-threaded concurrency, if you will.
零线程并发,如果你愿意的话。
When the operation completes, the method resumes after the await
- on a (possibly different) thread from the thread pool. 当操作完成时,该方法在
await
- 从线程池中的(可能不同的)线程上恢复。
In conclusion: async
scales better than threads, so there is definitely a benefit on the server side. 总结:
async
比线程更好,所以在服务器端肯定有一个好处。
More info: my own intro to async
post and this awesome video . 更多信息:我自己介绍
async
帖和这个很棒的视频 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.