IIS工作线程与Web应用程序线程 [英] IIS worker thread vs web application thread

查看:205
本文介绍了IIS工作线程与Web应用程序线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个ASP.NET Core Web应用程序,该应用程序需要重复运行一些后台线程.我知道这不是一个好的设计,但目前我必须以最小的努力来解决其主要问题.现在,我想知道是否应该担心是否通过Web服务器处理用户的HTTP请求?

问题很简单,但我找不到明确的答案:

在这样的应用程序中创建的线程之间有什么区别:

Task.Run(() => { // some parallel job })

和处理HTTP请求的IIS的工作线程?

它们是来自同一线程池还是驻留在单独的池中?

解决方案

根据

最大的区别是IIS知道它为传入请求创建的线程. IIS不知道您自己创建的任何线程.

当应用程序池被回收或IIS关闭时,它等待直到所有请求都已完成处理-等待它为每个请求创建的线程已完成处理-然后终止该进程.如果您创建了超出请求寿命的任何线程(例如,如果您创建了后台线程,然后将响应发送回客户端),则IIS不知道该线程仍在运行,并且随时可能杀死整个进程.

如果在所有线程完成之前不返回响应,那么您将不会遇到该特定问题.

另一个问题是,您可能达到允许的最大线程数.然后会发生各种奇怪的性能问题.但这取决于您正在创建多少个线程以及有多少个HTTP请求进入.

I'm maintaining an ASP.NET Core web application that needs to repeatedly run some background threads. I know it's not a good design but currently I have to fix its major issues with minimum effort. Now I wonder if I should worry about handling users http requests by web server or not?

Question is simple but I can't find any clear answer for it:

What is the difference between threads that are created in application like this:

Task.Run(() => { // some parallel job })

and worker threads of IIS that handle http requests?

Are they come from the same thread pool or they're reside in separate pools?

解决方案

According to this it's all one pool: "ASP.NET Core already runs app code on normal Thread Pool threads." In other words, there isn't a separate max for threads serving requests vs. background threads.

The biggest difference is that IIS is aware of the threads it creates itself for an incoming request. IIS is not aware of any threads you create yourself.

When an app pool is recycled, or IIS is shut down, it waits until all requests have finished processing - it waits until the threads it creates for each request has finished processing - and then it kills the process. If you create any threads that outlive the request (for example, if you create a background thread and then send the response back to the client) IIS has no idea that thread is still running and could kill the whole process at any time.

If you don't return a response until all the threads are complete, then you won't have that specific problem.

The other issue is that you may hit the maximum number of allowable threads. Then all kinds of weird performance issues would happen. But that depends on how many threads you are creating and how many HTTP requests are coming in.

这篇关于IIS工作线程与Web应用程序线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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