0

I'm learning about the AsyncController in ASP.NET MVC and using it with the TPL, but I'm struggling to see its need, I can understand when you would want to run an Action asynchronously to do something like send out an email, but in reality would you ever use it to return a view from an action?

For example if the Action gets some data from a database, which is set to work async, then return a View, if the data fails to retrieve in time will the View not just return with no data in the model?

2 Answers 2

3

Would you ever use it to return a view from an action?

The main advantage of asynchrony in ASP.NET is scalability. While the asynchronous work executes, you're not consuming any threads. This means your application will consume less memory and it may be also faster.

If the data fails to retrieve in time will the View not just return with no data in the model?

That depends on you and how exactly will you handle that failure.

Sign up to request clarification or add additional context in comments.

6 Comments

So to the second question, technically you wouldn't really want to use an async controller for most web calls because quite frankly there is a risk that the View will have nothing to return if you start doing prior work in an async fashion? What exactly are we preventing from being blocked by using the async functionality? Because other users hitting the site aren't exactly waiting for someone elses thread to complete are they?
@User101 If you reach the limit on the number of threads in the pool, then that's exactly what will happen: users will wait until a thread becomes available.
@User101 - You are not understanding how async requests work. They don't return to the user right away, they don't return "nothing" (unless you are doing it wrong). They only return when the async job is done, so there is no danger of "returning nothing". async is not about returning to the client, it's about giving up resources on the server.
@MystereMan thanks that clarifies it, so its more about spawning a separate parallel thread to ease the load off the pool that IIS has. In that case is svick's solution to increase the threadpool size an easier one than creating async controllers?
@User101 - Simply raising the thread pool size is seldom a good solution, since the more threads the system has to manage the more overhead there is in task switching. Also, remember that you have a finite amount of memory in a single process (2-3GB in a 32 bit process, for instance). Also, consider that each thread requires a minimum amount of memory. On very busy servers, you can quickly run out of resources.
|
2

Async controllers are used primarily to give up the current thread pool thread to allow other incoming connections to process work while you are waiting for a long running process to complete.

This has nothing to do with pass a view back. The process will still "block" from the end users perspective, but on the server the resources the server needs to respond to incoming requests will not be consumed.

By default, there are 250 thread pool threads per cpu core in an IIS worker process to respond to incoming connections (this can be tuned, but in general you should know what you're doing). If you have to people waiting for long requests to complete, then nobody else will be able to connect to your server until one of them finishes. Async controllers fix that problem.

You can also offload CPU bound work to a dedicated thread when using async controllers, where that was more difficult in synchronous controllers. And, it allows you to perform tasks in parallel. For instance, suppose you have to go out to 10 web sites and retrieve data. Most of the time is spent waiting for those web requests to return, and they can be done in parallel if you are doing things async.

6 Comments

I believe there used to be a limit of 25 threadpool threads per CPU in .Net 1. Currently, the default limit is much higher, something around a thousand.
Also, you seem to be confusing threads and processes. A single ASP.NET application will certainly execute in a single process. And using async controller doesn't “offload work to a dedicated thread”, it executes on a normal threadpool thread, just like usual.
@svick - Not sure where I got that 10 from.. it appears the default pool size is 250 threads per processor. I think 10 may have been the old ThreadPool class size. My comment about offloading to a non-worker thread was not well written, i'll revise.
@svick - I see what you mean about the confusion on threads and processes, I meant threads, not processes, but corrected now.
Okay, you can offload the work to another thread, but it won't actually give you anything. If the size of the threadpool is not enough, increase that size. I think that's a much better solution under normal circumstances.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.