什么是异步Web服务的好处时,没有代码的所有部分是异步 [英] What are the benefits of async webservices when not all parts of the code is async
问题描述
我想知道你使用异步HTTP请求,如果不是你的代码的所有部分是异步多少好处获得
I am wondering how much benefit you get from using async http requests if not all parts of your code is async.
让我们考虑到的情况:1)异步HTTP请求阻塞同步数据库调用,以及2)同步HTTP请求等待异步调用数据库。
Lets consider to scenarios: 1) async http requests blocking on sync database calls, and 2) sync http requests awaiting async database calls.
1)的Web API支持异步操作的方法,但如果我做一个同步数据库调用处理请求时,然后在调用的线程块,我不会得到更好的线程经济的异步能给我还是什么好处?
1) Web Api supports async action methods, but if I do a sync database call when handling the request, then the thread blocks on the call, and I will not get the benefits of better thread economy that async could give me or what?
2)如果我有一个同步ServiceStack服务呼叫等待着一个异步数据库调用,那么会发生什么?我假定一个线程被保留用于处理整个同步http请求,并且当该线程等待一个异步呼叫则它仍保留给web请求或什么?
2) If I have a synchronous ServiceStack service call that awaits an async database call, then what happens? I assume a thread is reserved for handling the entire sync http request, and when this thread awaits an async call then it is still reserved to the web request or what ?
Basicallly我的问题可以归结为:是否有任何理由使用异步如果不是所有的IO调用都是异步
Basicallly my question can be boiled down to this: Is there any reason to use async if not all IO calls are async?
推荐答案
在服务器端,有一个部分通常是没有任何好处。 - 异步
解决方案
On the server side, there is usually no benefit in a partially-async
solution.
1)的Web API支持异步操作的方法,但如果我做一个同步数据库调用处理请求时,则在电话会议上的线程块,我就没有得到更好的线程经济的异步能给我还是什么好处?
1) Web Api supports async action methods, but if I do a sync database call when handling the request, then the thread blocks on the call, and I will not get the benefits of better thread economy that async could give me or what?
正确的。只要你做一个同步(阻塞)数据库调用,你占用一个线程该呼叫的持续时间。所以,你会从得到异步
的好处不适用。
Correct. As soon as you do a synchronous (blocking) database call, you are tying up a thread for the duration of that call. So the benefits that you would have gotten from async
do not apply.
2 )如果我有一个同步ServiceStack服务呼叫等待着一个异步数据库调用,那么会发生什么?我假设一个线程被保留用于处理整个同步http请求,而当这个线程等待一个异步调用,然后它仍然保留web请求还是什么?
2) If I have a synchronous ServiceStack service call that awaits an async database call, then what happens? I assume a thread is reserved for handling the entire sync http request, and when this thread awaits an async call then it is still reserved to the web request or what ?
这仅仅是一样的同步数据库调用。在幕后,同步数据库调用异步执行,然后实际调用块调用线程,直到它完成。所以,你仍然有阻塞呼叫的持续时间线程,你没有得到任何异步
收益。
This is just the same as a synchronous database call. Under the covers, a synchronous database call executes the actual call asynchronously and then blocks the calling thread until it completes. So you still have a thread blocked for the duration of the call, and you don't get any async
benefits.
Basicallly我的问题可以归结为:是否有任何理由使用异步如果不是所有的IO调用都是异步
Basicallly my question can be boiled down to this: Is there any reason to use async if not all IO calls are async?
有一些比较模糊的场景。您可以使用 Task.WhenAll
做了有限型并发,这与比其他形式的更容易异步
异步并发。但是,这是我能想到的,如果你没有一个完全的唯一的好处 - 异步
栈
There are a few more obscure scenarios. You can use Task.WhenAll
to do a limited type of concurrency, which is much easier with async
than other forms of asynchronous concurrency. But that's the only benefit I can think of if you don't have a fully-async
stack.
这篇关于什么是异步Web服务的好处时,没有代码的所有部分是异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!