将方法放入任务中以避免阻塞 asp.net 线程

发布于 2024-11-04 19:29:02 字数 599 浏览 0 评论 0原文

我想知道以下代码在网络服务器上运行时是否有我不知道的问题。阅读优秀系列http://reedcopsey.com/series/parallelism-in-net4/< /a> 我无法找到任何与我的问题具体相关的内容,与 msdn 相同,所以我想我应该把它带到这里。

调用示例:

public ActionResult Index() {
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    Task.Factory.StartNew(() => {
        //This is some long completing task that I don't care about
        //Say logging to the database or updating certain information
        System.Threading.Thread.Sleep(10000);
    });

    return View();
}

I'm wondering if the following code has any gotcha's that I'm not aware of when running on a webserver. Reading through the excellent series http://reedcopsey.com/series/parallelism-in-net4/ I am unable to find anything that relates specifically to my question, same with the msdn, so I thought I'd bring it here.

Example call:

public ActionResult Index() {
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    Task.Factory.StartNew(() => {
        //This is some long completing task that I don't care about
        //Say logging to the database or updating certain information
        System.Threading.Thread.Sleep(10000);
    });

    return View();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫雨熙 2024-11-11 19:29:02

ASP.Net支持异步页面,参见ASP.NET中的异步页面,但是编程比较复杂模型并且完全不与 MVC 绑定。话虽这么说,从同步请求处理程序启动异步任务在一定程度上是有效的:

  • 如果请求添加新任务的速率超过处理过程的平均速率,您的进程最终将崩溃。任务会占用实时内存,最终它们将填满存储它们的内存队列,并且您将开始提交失败。
  • .Net Taks 本质上是不可靠的,因为它们缺乏持久存储,因此所有异步提交的任务都必须作为“废弃软件”进行线程化,即。如果它们从未完成,则应用程序或发出请求的用户不会有任何损失。如果任务很重要,那么它必须通过一种可靠的机制提交,以保证在出现故障时执行,就像 异步过程执行

ASP.Net supports asynchronous pages, see Asynchronous Pages in ASP.NET, but is a complicated programming model and does not bind at all with MVC. That being said, launching asynchronous tasks from a synchronous requests handler works up to a point:

  • if the rate at which requests add new tasks exceeds the average rate of processing your process will crash eventually. The Tasks take up live memory and eventually they will fill up the in memory queues where they're stored and your will start getting failures to submit.
  • .Net Taks are inherently unreliable as they lack a persistent storage, so all tasks that are submitted async must be threaded as 'abandonware', ie. if they never complete there is no loss to the application nor to the user making the request. If the task is important, then it must be submitted through a reliable mechanism that guarantees execution in the presence of failures, like the one presented in Asynchronous procedure execution.
宁愿没拥抱 2024-11-11 19:29:02

在这种情况下,一件重要的事情是确保任务中包含的代码包装在 try/catch 块中,否则该线程中抛出的任何可能的异常都会传播。您还应该确保在此长时间运行的任务中,您不会访问任何 Http Context 成员,例如请求、响应、会话...,因为当您访问它们时它们可能不再可用。

One important thing in this case is to ensure that the code contained inside the task is wrapped in a try/catch block or any possible exceptions thrown in this thread will propagate. You also should ensure that in this long running task you are not accessing any of the Http Context members such as Request, Response, Session, ... as they might no longer be available by the time you access them.

蹲墙角沉默 2024-11-11 19:29:02

使用new Thread而不是Task.Factory.StartNewTask.Factory.StartNew 使用线程池中的线程,如果您有许多后台任务,线程池将耗尽线程并降低您的 Web 应用程序的性能。请求将排队,您的 Web 应用程序最终将终止:)

您可以使用 Thread.CurrentThread.IsThreadPoolThread 测试您的后台工作是否在线程池上执行。如果得到 True,则使用线程池。

Use new Thread instead of Task.Factory.StartNew. Task.Factory.StartNew use thread from Threads Pool and if you will have many background tasks a Threads Pool will run out of threads and will degrade your web application. The requests will be queued and your web app eventually will die :)

You can test is your background work executed on Thread Pool using Thread.CurrentThread.IsThreadPoolThread. If you get True then Thread Pool is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文