使用任务或异步/等待中IHttpAsyncHandler [英] Using Task or async/await in IHttpAsyncHandler

查看:144
本文介绍了使用任务或异步/等待中IHttpAsyncHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于编写ASP.NET应用程序的开始时十分,当我想添加一个线程有3个简单的方法,我可以我的ASP.NET应用程序中实现线程:

Since the very begining of writing ASP.NET applications when I wanted to add a threading there are 3 simple ways I can accomplish threading within my ASP.NET application :


  • 使用 System.Threading.ThreadPool

  • 使用自定义委托,并调用它的的BeginInvoke 方法。

  • 使用自定义线程与 System.Threading.Thread 类的帮助。

  • Using the System.Threading.ThreadPool.
  • Using a custom delegate and calling its BeginInvoke method.
  • Using custom threads with the aid of System.Threading.Thread class.

前两种方法提供了一种快速的方法来火了工作线程的应用程序。但不幸的是,他们伤害了你的应用程序的整体性能,因为它们的从消费ASP.NET使用处理HTTP请求相同的线程池

The first two methods offer a quick way to fire off worker threads for your application. But unfortunately, they hurt the overall performance of your application since they consume threads from the same pool used by ASP.NET to handle HTTP requests.

然后我想用一个新的任务或异步/等待写 IHttpAsyncHandler 。你可以找到一个例子就是德鲁马什解释在这里: http://stackoverflow.com/a/6389323/261950

Then I wanted to use a new Task or async/await to write IHttpAsyncHandler. One example you can find is what Drew Marsh explains here : http://stackoverflow.com/a/6389323/261950

我的猜测是,使用任务或异步/等待仍然使用来自ASP.NET线程池的线程,我不希望为明显的原因。

My guess is that using Task or async/await still consume the thread from the ASP.NET thread pool and I don't want for the obvious reason.

你能告诉我如果我能在后台线程使用任务(异步/等待) System.Threading.Thread 并没有从线程池

在此先感谢您的帮助。

托马斯

推荐答案

我一直在通过互联网寻找信息的几天。让我总结一下我发现到现在为止:

I've been looking for information through internet for a couple of days. Let me sum up what I found until now :

ASP.NET线程池的事实


  • 由于安德烈斯说:当异步/的await不会消耗额外的线程池线程?只有在情况下,你正在使用BCL异步方法。一个使用IOCP线程来执行IO约束操作。

安德烈斯继续...的如果你想为异步执行一些同步code或您自己的库code,即code会可能的使用额外的线程池的线程,除非你明确地使用IOCP线程池或您自己的线程池。

Andres continues with ...If you are trying to async execute some sync code or your own library code, that code will probably use an additional ThreadPool thread unless you explicitely use the IOCP ThreadPool or your own ThreadPool.

不过,据我知道你不能选择whetever要使用IOCP线程,并作出正确的执行线程池是不值得的。我怀疑有人做已经存在一个更好的。

But as far as I know you can't chose whetever you want to use a IOCP thread, and making correct implementation of the threadPool is not worth the effort. I doubt someone does a better one that already exists.


  • ASP.NET使用线程公共语言运行库(CLR)线程池来处理请求。只要有线程池中可用线程,ASP.NET毫不费力调度传入的请求。

  • ASP.NET uses threads from a common language runtime (CLR) thread pool to process requests. As long as there are threads available in the thread pool, ASP.NET has no trouble dispatching incoming requests.

异步代表使用线程线程池。

当你要开始考虑实施异步执行?


  • 在您的应用程序执行相对较长的I / O操作(数据库查询,Web服务调用,和其他I / O操作)

  • When your application performs relatively lengthy I/O operations (database queries, Web service calls, and other I/O operations)

如果你想要做的I / O工作,那么你就应该使用I / O线(I / O完成端口),并明确你应该使用以任何库班你是支持异步回调使用。他们的名字开始的话开始结束

If you want to do I/O work, then you should be using an I/O thread (I/O Completion Port) and specifically you should be using the async callbacks supported by whatever library class you're using. Theirs names start with the words Begin and End.

如果请求计算便宜过程,那么并行可能是一个不必要的开销。

If requests are computationally cheap to process, then parallelism is probably an unnecessary overhead.

如果传入的请求率高,再加入更多的并行可能会产生一些好处,实际上可能会降低性能,因为工作的传入率可能高到足以使CPU的忙。

If the incoming request rate is high, then adding more parallelism will likely yield few benefits and could actually decrease performance, since the incoming rate of work may be high enough to keep the CPUs busy.

我应该创建新的线程?


  • 避免创建新线程就像你会避免瘟疫。

  • Avoid creating new threads like you would avoid the plague.

如果您实际上从处理其他请求排队足够的工作项目prevent ASP.NET,那么你应该挨饿线程池!如果你是在同一时间运行字面上数百个CPU密集型操作,它会做什么好事,有另一个工作线程服务ASP.NET请求,当机器已经超载。

If you are actually queuing enough work items to prevent ASP.NET from processing further requests, then you should be starving the thread pool! If you are running literally hundreds of CPU-intensive operations at the same time, what good would it do to have another worker thread to serve an ASP.NET request, when the machine is already overloaded.

而TPL?


  • TPL能适应过程中利用现有资源。如果服务器已经加载,第三方物流可以用短短的一个工人,并向前发展。如果服务器大多是免费的,他们可以长到使用尽可能多的工人线程池可以备用。

  • TPL can adapt to use available resources within a process. If the server is already loaded, the TPL can use as little as one worker and make forward progress. If the server is mostly free, they can grow to use as many workers as the ThreadPool can spare.

任务使用线程池线程执行。

Tasks use threadpool threads to execute.

参考

  • http://msdn.microsoft.com/en-us/magazine/cc163463.aspx
  • http://blogs.msdn.com/b/pfxteam/archive/2010/02/08/9960003.aspx
  • http://stackoverflow.com/a/2642789/261950

这篇关于使用任务或异步/等待中IHttpAsyncHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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