7

One of the reasons async was praised for ASP.NET was to follow Nodejs async platform which led to more scalability with the freeing up of threads to handle subsequent requests etc.

However, I've since read that wrapping CPU-bound code in Task.Run will have the opposite effect i.e. add even more overhead to a server and use more threads.

Apparently, only true async operations will benefit e.g. making a web-request or a call to a database.

So, my question is as follows. Is there any clear guidance out there as to when action methods should be async?

onefootswill
  • 3,707
  • 6
  • 47
  • 101
  • 1
    IMHO, one of the best sources of info on `async-await`, [Stephen Cleary](http://stackoverflow.com/a/30574578/304683). Hth – EdSF Mar 06 '17 at 04:54
  • @EdSF Thanks. It was actually Stephen Cleary's blog that I have been reading. Mr Cleary is the one who opined about the fruitlessness of wrapping CPU-bound operations in async code. I'll have a read of the SO answer you linked to and see if I learn anything new there. Thanks. – onefootswill Mar 06 '17 at 05:34
  • @onefootswill link articles you read about fruitlessness of CPU bound operations in async code? – Don Cheadle Jun 26 '18 at 02:54
  • @mmcrae This article is the one where Stephen discusses Fake Async - https://blog.stephencleary.com/2013/10/taskrun-etiquette-and-proper-usage.html Cheers – onefootswill Jun 26 '18 at 03:28

2 Answers2

7

Mr Cleary is the one who opined about the fruitlessness of wrapping CPU-bound operations in async code.

Not exactly, there is a difference between wrapping CPU-bound async code in an ASP.NET app and doing that in a - for example - WPF desktop app. Let me use this statement of yours to build my answer upon.

You should categorize the async operations in your mind (in its simplest form) as such:

  • ASP.NET async methods, and among those:
    • CPU-bound operations,
    • Blocking operations, such as IO-operations
  • Async methods in a directly user-facing application, among those, again:
    • CPU-bound operations,
    • and blocking operations, such as IO-operations.

I assume that by reading Stephen Cleary's posts you've already got to understand that the way async operations work is that when you are doing a CPU-bound operation then that operation is passed on to a thread pool thread and upon completion, the program control returns to the thread it was started from (unless you do a .ConfigureAwait(false) call). Of course this only happens if there actually is an asynchronous operation to do, as I was wondering myself as well in this question.

When it comes to blocking operations on the other hand, things are a bit different. In this case, when the thread from which the code performed asynchronously gets blocked, then the runtime notices it, and "suspends" the work being done by that thread: saves all state so it can continue later on and then that thread is used to perform other operations. When the blocking operation is ready - for example, the answer to a network call has arrived - then (I don't exactly know how it is handled or noticed by the runtime, but I am trying to provide you with a high-level explanation, so it is not absolutely necessary) the runtime knows that the operation you initiated is ready to continue, the state is restored and your code can continue to run.

With these said, there is an important difference between the two:

  • In the CPU-bound case, even if you start an operation asynchronously, there is work to do, your code does not have to wait for anything.
  • In the IO-bound case or blocking case, however, there might be some time during which your code simply cannot do anything but wait and therefore it is useful that you can release that thread that has done the processing up until that point and do other work (perhaps process another request) meanwhile using it.

When it comes to a directly-user-facing application, for example, a WPF app, if you are performing a long-running CPU-operation on the main thread (GUI thread), then the GUI thread is obviously busy and therefore appears unresponsive towards the user because any interaction that is normally handled by the GUI thread just gets queued up in the message queue and doesn't get processed until the CPU-bound operation finishes.

In the case of an ASP.NET app, however, this is not an issue, because the application does not directly face the user, so he does not see that it is unresponsive. Why you don't gain anything by delegating the work to another thread is because that would still occupy a thread, that could otherwise do other work, because, well, whatever needs to be done must be done, it just cannot magically be done for you.

Think of it the following way: you are in a kitchen with a friend (you and your friend are one-one threads). You two are preparing food being ordered. You can tell your friend to dice up onions, and even though you free yourself from dicing onions, and can deal with the seasoning of the meat, your friend gets busy by dicing the onions and so he cannot do the seasoning of the meat meanwhile. If you hadn't delegated the work of dicing onions to him (which you already started) but let him do the seasoning, the same work would have been done, except that you would have saved a bit of time because you wouldn't have needed to swap the working context (the cutting boards and knives in this example). So simply put, you are just causing a bit of overhead by swapping contexts whereas the issue of unresponsiveness is invisible towards the user. (The customer neither sees nor cares which of you do which work as long as (s)he receives the result).

With that said, the categorization I've outlined at the top could be refined by replacing ASP.NET applications with "whatever application has no directly visible interface towards the user and therefore cannot appear unresponsive towards them".

Balázs
  • 2,929
  • 2
  • 19
  • 34
  • Thanks. I liked your wording "simply cannot do anything but wait". I guess I am wondering why Nodejs is scaleable, where every thread that handles requests hands off processing to another. Regardless of whether it will be blocking IO or CPU bound. It seems that there is not a great cause for async on the server, as I rarely perform WebRequests from my server. The jury seems to be out on whether to use EF's async features. I've read just as much bad as good. – onefootswill Mar 06 '17 at 20:53
5

ASP.NET requests are handled in thread pool threads. So are CPU-bound async operations (Task.Run).

Dispatching async calls to a thread pool thread in ASP.NET will result in returning a thread pool thread to the thread pool and getting a another. one to run the code and, in the end, returning that thread to the thread pool and getting a another one to get back to the ASP.NET context. There will be a lot of thread switching, thread pool management and ASP.NET context management going on that will make that request (and the whole application) slower.

Most of the times one comes up with a reason to do this on a web application ends up being because the web applications is doing something that it shouldn't.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59