玩2!框架多线程问题 [英] Play 2! framework multithreading issue

查看:137
本文介绍了玩2!框架多线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有标准比赛2!通过JSON与前端通信的应用程序. 所有请求都路由到控制器.现在,控制器会导入一些重型计算类.

I have a standard play 2! application which communicates with the front-end via JSON. All requests are routed to controllers. Now the controllers import some heavy duty calculation classes.

因此,如果用户请求计算,则控制器方法可能需要5秒钟才能从实际的计算类中获取结果,因为该计算是迭代的并且需要时间.

So if a user requests a calculation, it might take 5 secs for the controller method to get the results back from the actual calculation class, as the calculation is iterative and takes time.

这不是意味着服务器在这5秒钟内被阻止了吗?它不能服务任何其他用户吗?如果有20个人同时使用此计算工具,由于排长队,其他人甚至很难加载同一服务器提供的主页... 我在这种思路上正确吗?有什么可能的解决方案?

Doesn't this mean then that the server is blocked for these 5 secs? It cannot service any other user? Is 20 people are using this calculation tool simultaneously, it could become difficult for others to even load the home page served by same server due to the long queue... Am i right in this line of thinking and what could be a possible solution?

推荐答案

您的假设是正确的. 播放2使用了几个线程池,每个线程池都分配给特定的东西.请参见此处

Your assumptions are correct. Play 2 uses a few thread pools which are each allocated to specific things. See here

就像您说的那样,使用默认配置并假设您正在同步调用计算类,如果同时有超过24个人访问您的网站,则Play 2应用将开始被阻止.

So like you said, with the default configuration and assuming you are calling your calculation class synchronously, if more than 24 people hit your site at the same time, you Play 2 app will start to block.

我特别提到了24个人,因为Play的在此处查看了解更多选项.因此,如果您阻止所有24个线程,则将在站点响应能力方面开始出现问题.

I specifically mentioned 24 people because Play's default thread pool configuration is set to 1 thread per core up to a max of 24 threads. See here for more options. Hence if you block all 24 threads you're going to start having problems in terms of site responsiveness.

有一些解决方案:

  • Increase the default thread pool size (if you have a good idea of what your load will be) See here for more about that
  • Create a separate thread pool/execution context specifically (see docs on "Using other thread pools") for your heavy computation class and call it in a promise/future providing that execution context. This way the default thread pool will not be burdened with that blocking call. Again, you will have to tune that pool to your requirements.
  • Implement your heavy computation functionality as Akka Actors. See here (probably the best solution if you have the time)

这篇关于玩2!框架多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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