异步I/O方法如何处理 [英] How are asynchronous I/O methods processed

查看:76
本文介绍了异步I/O方法如何处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大量阅读了有关异步等待的内容之后,我只能在GUI线程(WPF/WinForms)中找到使用它的好处.

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

在什么情况下会减少WCF服务中线程的创建? 程序员是否必须通过选择在Web服务中实现async-await来对服务中的每个方法 使用async-await?在充满async-await的服务中制作一些非async-await方法会重新使用我的服务效率吗?怎么样?

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?

最后一个问题-有人说使用'await Task.Run(()=> ...)'不是真正的异步等待".这么说是什么意思?

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

感谢前进,

两个答案都是非常好的,但是对于北斗七星关于异步等待如何工作的解释,我建议在这里阅读@Stephen Cleary答案: https://stackoverflow.com/a/7663734/806963

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

以下主题是了解他的答案所必需的: SynchronizationContext,SynchronizationContext.Current,TaskScheduler,TaskScheduler.Current,线程池.

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

推荐答案

在服务器应用程序(如WCF)中进行异步/等待的真正好处是异步I/O.

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

当您调用同步I/O方法时,调用线程将被阻塞,等待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...

如果使用异步IO,则不会阻塞线程.启动异步IO操作后,线程池再次可以使用它.异步操作完成后,线程池将分配一个线程以继续处理请求.没有资源浪费.

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.

来自 MSDN (关于文件I/O,但也适用于其他文件)

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

在同步文件I/O中,线程启动I/O操作并立即进入等待状态,直到I/O请求完成.执行异步文件I/O的线程通过调用适当的函数向内核发送I/O请求.如果内核接受了该请求,则调用线程将继续处理另一个作业,直到内核向线程发出I/O操作已完成的信号.然后,它会中断其当前作业,并根据需要处理来自I/O操作的数据.

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.

现在您可能会明白,如果任务中的IO是同步完成的,为什么await Task.Run()不会带来任何好处.无论如何,一个线程都会被阻塞,而不是被称为Task.Run()的线程.

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().

您无需异步实现每种方法即可看到性能的提高(尽管它总是习惯于异步执行I/O).

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).

这篇关于异步I/O方法如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆