Using Task or async/await in IHttpAsyncHandler

后端 未结 6 769
名媛妹妹
名媛妹妹 2020-12-23 12:22

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 :

6条回答
  •  生来不讨喜
    2020-12-23 13:15

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

    ASP.NET ThreadPool facts

    • As Andres said: When async/await will not consume an additional ThreadPool thread ? Only in the case you are using BCL Async methods. that uses an IOCP thread to execute the IO bound operation.

    • 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.

    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 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.

    • Async delegates use the threads from ThreadPool.

    When you should start thinking about implementing asynchronous execution ?

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

    • 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.

    • 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.

    Should I create new Threads ?

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

    • 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.

    And the 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.

    References

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

提交回复
热议问题