How are asynchronous I/O methods processed

烂漫一生 提交于 2019-12-24 14:48:57

问题


After reading alot about async-await, I can only find the benefits of using it in GUI thread (WPF/WinForms).

In what scenarios does it reduce the creation of threads in WCF services? Does a programmer must use async-await on every method in the service by choosing to implement async-await in web service? Making some non-async-await methods in a service full of async-await reduse the efficiency of my service? How?

Last question - some say that using 'await Task.Run(()=>...)' is not a "real async-await". What do they mean by saying that?

Thanks in advence, Stav.

EDIT:

Both answers are excellent but for even dipper explanation about how async-await works, I suggest to read @Stephen Cleary answer here: https://stackoverflow.com/a/7663734/806963

Following topics are required for understand his answer: SynchronizationContext,SynchronizationContext.Current,TaskScheduler,TaskScheduler.Current,Threadpool.


回答1:


The real benefit of async/await in server applications (like WCF) is asynchronous I/O.

When you call a synchronous I/O method, the calling thread will be blocked waiting for the I/O to complete. The thread cannot be used by other requests, it just waits for the result. When more requests arrive, the thread pool will create more threads to handle them, wasting a lot of resources - memory, context switching when the waiting threads get unblocked...

If you use async IO, the thread is not blocked. After starting the asynchronous IO operation, it is again available to be used by the thread pool. When the async operation is finished, the thread pool assigns a thread to continue processing the request. No resources wasted.

From MSDN (it's about file I/O, but applies to other too)

In synchronous file I/O, a thread starts an I/O operation and immediately enters a wait state until the I/O request has completed. A thread performing asynchronous file I/O sends an I/O request to the kernel by calling an appropriate function. If the request is accepted by the kernel, the calling thread continues processing another job until the kernel signals to the thread that the I/O operation is complete. It then interrupts its current job and processes the data from the I/O operation as necessary.

Now you probably can see why await Task.Run() will not give any benefit if the IO in the task is done synchronously. A thread will get blocked anyway, just not the one that called the Task.Run().

You don't need to implement every method asynchronously to see improvement in performance (although it should become a habit to always perform I/O asynchronously).




回答2:


In what scenarios does it reduce the creation of threads in WCF services?

If you have an action that will wait on an IO operation (reading from the database, calling an external web service, ...), using async/await frees up the managed thread that your WCF request is being processed on. That makes the thread available for other requests, pending completion of your IO. It makes for more efficient use of the thread pool.

After reading alot about async-await, I can only find the benefits of using it in GUI thread

For client applications that is the key benefit that I'm aware of, since you are far less likely to run out of manged threads than you are in a server application.

some say that using 'await Task.Run(()=>...)' is not a "real async-await".

You allocate a new managed thread to run your new task, so you are not saving any managed threads.



来源:https://stackoverflow.com/questions/33027364/how-are-asynchronous-i-o-methods-processed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!