使用异步等待是否可以避免线程耗尽?

编程入门 行业动态 更新时间:2024-10-25 04:25:17
本文介绍了使用异步等待是否可以避免线程耗尽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在对 .NET Core API 终结点上的以下性能问题进行故障排除:

We are troubleshooting the following performance issues on a .NET Core API endpoint:

  • 端点在较小负载下持续返回的时间少于 500MS .
  • 当我们通过3个浏览器访问端点时,每秒发出一个请求,它会变慢(在添加第三个浏览器进行通话的一分钟内,响应时间下降到 50,000MS或更糟.
  • 每个其他浏览器添加API使用的线程,例如基本线程数为40,第二个浏览器命中端点将导致52个线程,第三个峰值为70,依此类推.
  • 加载一个端点后,整个API会缓慢返回(所有端点).这是我与#3"一起思考线程耗尽"的主要原因.
  • The endpoint consistently returns in less than 500MS under minor load.
  • When we hit the endpoint from 3 browsers, with one request a second, it gets progressively slower (within a minute of adding a third browser making calls, response times drops to 50,000MS or worse.
  • Each additional browser adds threads used by the API, e.g. 40 threads base, 2nd browser hitting endpoint leads to 52 threads, third spikes to 70, and so on.
  • When one endpoint is loaded, the entire API returns slowly (all endpoints). This is my main reason for thinking "thread exhaustion", along with point #3.
  • 当前代码如下:

    public IActionResult GetPresentationByEvent(int eventid) { return Authorized(authDto => { var eventList = _eventService.GetPresentationByEvent(eventid); return Ok(eventList) }) }

    我的理论是 return Authorized(authDto => 会保留一个线程,直到返回为止,这会导致线程耗尽.

    My theory is that return Authorized(authDto => holds a thread until it returns, leading to thread exhaustion.

    public async Task<IActionResult> GetPresentationByEvent(int eventid) { return Authorized(async authDto => { Task<List<whatever>> eventList = _eventService.GetPresentationByEvent(eventid); return Ok(eventList) } }

    Authorized 是第三方库的一部分,所以我很难对此进行测试.想知道这是否是一个可能的问题/解决方案.

    Authorized is part of a third-party library, so I can't test this easily. Would like to know if this looks like a likely problem/solution.

    推荐答案

    是的,异步等待可以减少线程耗尽.简而言之,当您生成的任务超出ThreadPool的处理能力时,就会出现线程耗尽.

    Yes async await can reduce thread exhaustion. In a few words thread exhaustion arise when you generate more tasks than your ThreadPool can handle.

    您可以在此处检查细微的特殊性:线程饥饿和排队

    There are subtle specifities that you can check here : Thread starvation and queuing

    唯一需要牢记的是,您永远都不应阻塞任务内部.这意味着在异步等待状态下调用异步代码(并且永远不要在未完成的任务上使用.Wait或.Result).

    The only thing that you have to keep in mind on your side is that you should never block inside a task. This implies calling asynchronous code with async await (and never using .Wait or .Result on a non finished task).

    如果使用的阻塞代码未使用异步等待模式,则必须在专用线程(而不是任务线程队列)上生成它.

    If you use some blocking code wich is not using the async await pattern you have to spawn it on a dedicated thread (not the task thread queue).

    更多推荐

    使用异步等待是否可以避免线程耗尽?

    本文发布于:2023-10-09 06:24:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1474954.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:线程

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!