哪个最好遵循mvc中的异步视图或异步控制器? [英] Which is best to follow asynchronous views or asynchornous controllers in mvc?

查看:84
本文介绍了哪个最好遵循mvc中的异步视图或异步控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mvc的新手。

我看过很少有关于异步视图的文章,很少有关于异步控制器的文章。

使用哪些文章可以获得最佳性能。每个的优点和缺点是什么?

Hi, I am newbie in mvc.
I have seen few articles on asynchronous views and few on asynchronous controllers.
Usage of which ones gives the best performance. what are the pros and cons of each?

推荐答案

MVC 4中引入的任务并行库(TPL)允许您处理池中的并发请求。没有TPL的ASP.Net受限于它可以通过线程池中的线程数同时处理的请求数。使用TPL的ASP.Net受机器处理请求的CPU /内存/ IO限制。



TPL不会像你看来那样消耗线程认为它确实如此。任务不是线程,它们是某些计算单元的包装器。任务调度程序负责在任何不忙的线程上执行每个任务。在运行时,等待任务不会阻塞线程,它只是暂停执行状态,以便以后可以继续。



正常情况下,单个HTTP请求将由单个线程处理,从池中完全删除该线程,直到返回响应。拥有TPL,您不受此约束的约束。任何进入的请求都会启动计算能够在池中任何线程上执行的响应所需的每个计算单元的延续。使用此模型,您可以处理比标准ASP.Net更多的并发请求。





AsyncController类使您可以编写异步行动方法。您可以对长时间运行的非CPU绑定请求使用异步操作方法。这可以避免在处理请求时阻止Web服务器执行工作。





异步操作仅在您执行时有用I / O绑定操作,例如远程服务器调用。异步调用的好处是在I / O操作期间,没有使用ASP.NET工作线程。对于登录操作的同步和异步操作,请考虑以下内容:



a。当请求到达操作时,ASP.NET从线程池中获取一个线程并开始执行它。

b。调用IdentityManager.Authentication.CheckPasswordAndSignIn方法。这是一个阻塞调用,在整个调用期间,工作线程正在受到危害。



以下是第二个请求的工作方式:



当请求到达操作时,ASP.NET从线程池中获取一个线程并开始执行它。



a。调用IdentityManager.Authentication.CheckPasswordAndSignInAsync,立即返回。注册了I / O完成端口,并将ASP.NET工作线程释放到线程池。

b。稍后当操作完成时,将发出I / O完成端口信号,从线程池中抽取另一个线程以完成返回视图。



如您所见,第二种情况ASP.NET工作线程仅在短时间内使用,这意味着池中有更多线程可用于服务其他请求。



顺便说一下,异步视图 - 我还没有听说过这些?



你可以在一个简单的例子中找到: Web API /ASP.NET MVC中的异步编程 [ ^ ]



希望这会有所帮助。



Mostafa
Task Parallel Library(TPL) that has been introduced in MVC 4 allow you to handle concurrent requests within a pool. ASP.Net without TPL is limited in the number of requests it can handle concurrently by the number of threads in a thread pool. ASP.Net that uses the TPL is limited by the CPU/memory/IO of the machine handling requests.

The TPL does not consume threads in the way you seem to think it does. Tasks are not threads, they are a wrapper around some unit of computation. The task scheduler is responsible for executing each task on any thread that is not busy. At runtime, awaiting a task does not block the thread, it merely parks the execution state so that it may proceed at a later time.

In normal case, a single HTTP request would be handled by a single thread, completely removing that thread from the pool until a response is returned. Having the TPL, you are not bound by this constraint. Any request that come in starts a continuation with each unit of computation required to calculate a response able to execute on any thread in the pool. With this model, you can handle many more concurrent requests than with standard ASP.Net.


The AsyncController class enables you to write asynchronous action methods. You can use asynchronous action methods for long-running, non-CPU bound requests. This avoids blocking the Web server from performing work while the request is being processed.


The async actions are useful only when you are performing I/O bound operations such as remote server calls. The benefit of the async call is that during the I/O operation, no ASP.NET worker thread is being used. Consider the following for both sync and async operation for a login action:

a. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
b. The IdentityManager.Authentication.CheckPasswordAndSignIn method is invoked. This is a blocking call and during the entire call the worker thread is being jeopardized.

And here's how the 2nd request works:

When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.

a. The IdentityManager.Authentication.CheckPasswordAndSignInAsync is called which returns immediately. An I/O Completion Port is registered and the ASP.NET worker thread is released to the thread pool.
b. Later when the operation completes, the I/O Completion port is signaled, another thread is drawn from the thread pool to finish returning the view.

As you can observe, the 2nd case ASP.NET worker threads are used only for short period of time which means there are more threads available in the pool for serving other requests.

By the way, asynchronous views - I haven't heard about these?

You can follow a simple example at : Asynchronous Programming in Web API /ASP.NET MVC[^]

Hope this will help.

Mostafa


这篇关于哪个最好遵循mvc中的异步视图或异步控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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