如何在Playframework中配置ThreadPool优先级 [英] How to configure ThreadPool priority in Playframework

查看:92
本文介绍了如何在Playframework中配置ThreadPool优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在系统中有2个ExecutionContext(Scala 2.11.4,Playframework 2.3.7)

We have 2 ExecutionContexts in the system (Scala 2.11.4, Playframework 2.3.7)

  1. 主要上下文-用于系统操作(使用主要Play ExecutionContext).
  2. 管理性-用于后端相关任务.

我已在application.configurations中将它们分成2个.

I've separated those into 2 in application.configurations.

play {
  akka {
    akka.loggers = ["akka.event.slf4j.Slf4jLogger"]
    loglevel = DEBUG
    actor {
      default-dispatcher = {
        fork-join-executor {
          parallelism-min = 20
          parallelism-max = 20
        }
      }
    }
  }
}

contexts {
  admin {
    fork-join-executor {
      parallelism-min = 30
      parallelism-max = 30
    }
  }
}

是否可以配置ThreadPool优先级?

Is there a way to configure ThreadPool priority?

推荐答案

当前无法配置线程优先级,因为该设置在许多平台上几乎没有影响,因此可以用作安慰剂.如果您启动并积极使用的线程数超过了CPU内核数,那么最终的竞争将是昂贵的,并且会浪费资源,因此,最好将线程池大小仔细调整为与CPU绑定的部分可用的内核数.

There is currently no way to configure thread priority because that setting is mostly without effect on many platforms and as such qualifies as a placebo. If you start and actively use more threads than you have CPU cores then the resulting competition will be costly and will waste resources, so you will be better off carefully fitting your threadpool sizes to the available cores for the CPU-bound part.

发布的配置示例让我感到奇怪:系统真的有30个以上的内核吗?否则,contexts.admin调度程序将无法从优先级中获​​利.

The posted configuration example makes me wonder: does the system really have more than 30 cores? Otherwise the contexts.admin dispatcher would not be able to profit from prioritization anyway.

最后,只有一种方法可以为特定任务保留CPU时间,那就是永远不要将这些核心用于任何其他任务(如果您正在编写低级C代码并使用Unix实时优先级,但这是容易出错的,并不是出于虚弱之心-犯错误将使您无法进入系统.

In the end there is only exactly one way to reserve CPU time for a specific task, and that is to never give those cores to any other task (there would be other ways if you were writing low-level C code and using Unix realtime priorities, but that is error-prone and not for the faint of heart—making a mistake will lock you out of the system).

这意味着,在具有24个内核的系统上,该系统应该处理一些网络流量,与一些DB对话并处理一些数字,例如,我将为数字处理创建一个大小为16的固定线程池,为该计算机创建一个大小为16的固定线程池.阻止DB调用(根据最适合该DB的连接数确定大小,例如40),再加上Play和Akka的常规调度程序,这些调度程序处理网络和其他杂项(既不阻塞也不占用CPU).线程优先级不包含在此图中.

This means that on a system with 24 cores that is supposed to handle some network traffic, talk to some DB and crunch some numbers, I would for example create a fixed threadpool of size 16 for the number crunching and a threadpool for the blocking DB calls (sized according to the number of connections that works best for that DB, say 40) plus the normal dispatchers for Play and Akka that handle the network and miscellaneous other stuff (that is neither blocking nor CPU intensive). Thread priorities do not factor into this picture.

这篇关于如何在Playframework中配置ThreadPool优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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