ASP.NET 4 中是否可以执行并发处理?
我想问一下ASP.NET 4中是否可以使用并行编程?例如 PLINQ。
(因为现在所有托管服务器都是多核的,它会提供更好的性能吗?)
I'd like to ask you is it possible to use parallel programming in ASP.NET 4? For example PLINQ.
(Because all hosting servers are multi-cores now and will it give better perfomance?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。但在大多数情况下,这是否有意义值得怀疑。 ASP.NET 已经高度并行化,因为每个请求都在它自己的线程中工作。如果您分拆其他线程来完成某些工作,则会产生开销。这种开销会减慢处理其他请求的其他线程的速度。然而,在同步结果以完成请求时,您将引入另一个开销。此外,这种开销可能会减慢响应请求所需的时间。
在某些情况下,这实际上有助于提高整体性能,但我认为总的来说这是不值得的。
当然,只有使用这两种方法进行压力测试才能确定使用 PLINQ 还是不使用它是否更有效。
Yes it's possible. But in doubt it makes sense in the most cases. ASP.NET is already highly parallelized, as every request works in it's own thread. If you spin off other threads to do some of the work, that would create overhead. This overhead will slow down other threads working on other requests. Then again, you will introduce another overhead when synchronizing the results to finish the request. Also this overhead will probably slow down the time required to answer the request.
There might be scenarios where that actually would help increase overall performance, but I think in general it's not worth it.
Of course only stress tests with both approaches would make sure if its more efficient to go with PLINQ or to not use it.
答案是肯定的。
你为什么会有不同的想法呢? (因此出现了讽刺性的评论。)
请注意,除非您采取特殊步骤,否则每个 HTTP 请求都需要在开始为其提供服务的线程上完成。特殊步骤包括告诉 ASP.NET 对页面使用异步处理,这允许在不同的线程上创建响应并完成请求(可能还需要中间处理其他线程)。如果您从请求的原始线程使用 TPL(包括 PLINQ),这不是问题。
The answer is YES.
Why would you think otherwise? (Hence the sarcastic comments.)
Note, unless you take special steps every HTTP request needs to be completed on the thread that starts serving it. The special steps involve telling ASP.NET to use asynchronous processing for pages which allows a response to be created and the request to be completed on a different threads (with, potentially, intermediate processing other other threads). If you use TPL (including PLINQ) from the request's original thread this is not a problem.