加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何使用正确的线程池调整Play Framework应用程序?

发布时间:2020-12-16 18:59:00 所属栏目:安全 来源:网络整理
导读:我正在使用Play Framework( Scala)2.3版.来自文档: You can’t magically turn synchronous IO into asynchronous by wrapping it in a Future. If you can’t change the application’s architecture to avoid blocking operations,at some point that op
我正在使用Play Framework( Scala)2.3版.来自文档:

You can’t magically turn synchronous IO into asynchronous by wrapping it in a Future. If you can’t change the application’s architecture to avoid blocking operations,at some point that operation will have to be executed,and that thread is going to block. So in addition to enclosing the operation in a Future,it’s necessary to configure it to run in a separate execution context that has been configured with enough threads to deal with the expected concurrency.

这让我对如何调整我的webapp有点困惑.具体来说,由于我的应用程序有大量的阻塞调用:混合使用JDBC调用,以及使用阻塞SDK调用第三方服务,配置执行上下文和确定要提供的线程数的策略是什么?我需要一个单独的执行上下文吗?为什么我不能简单地将默认池配置为具有足够数量的线程(如果我这样做,为什么还需要将调用包装在Future中?)?

我知道这最终将取决于我的应用程序的细节,但我正在寻找关于策略和方法的一些指导.游戏文档鼓励在任何地方使用非阻塞操作,但实际上典型的网络应用程序命中sql数据库有很多阻止调用,我从阅读文档中得到的印象,这种类型的应用程序将执行远远不是最佳的默认配置.

解决方法

[…] what is the strategy for configuring the execution context and
determining the number of threads to provide

那么,这是一个棘手的部分,取决于您的个人要求.

>首先,您可能应该选择docs的基本配置文件(纯异步,高度同步或许多特定的线程池)
>第二步是通过分析和基准测试应用程序来微调您的设置

Do I need a separate execution context?

不必要.但是,如果要一次触发所有阻塞IO调用而不是顺序触发,则使用单独的执行上下文是有意义的(因此数据库调用B不必等到数据库调用A完成).

Why can’t I simply configure the default pool to have a sufficient
amount of threads (and if I do this,why would I still need to wrap
the calls in a Future?)?

你可以,查看docs:

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

通过这种方法,您基本上可以将Play转换为每个请求模型的单线程.这不是Play背后的想法,但如果您正在进行大量阻止IO调用,那么这是最简单的方法.在这种情况下,您不需要在Future中包装数据库调用.

简而言之,您基本上有三种方法:

>仅使用其API调用是非阻塞和异步的(IO-)技术.这允许您使用适合Play性质的小线程池/默认执行上下文>通过大幅增加默认执行上下文,将Play转换为每个请求一个线程的框架.不需要期货,只需像往常一样调用您的阻止数据库>为阻塞IO调用创建特定的执行上下文,并对您正在执行的操作进行细粒度控制

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读