我应该覆盖默认的ExecutionContext吗? [英] Should I override the default ExecutionContext?

查看:133
本文介绍了我应该覆盖默认的ExecutionContext吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在scala中使用futures时,默认行为是使用默认的Implicits.global执行上下文。似乎这默认使每个处理器一个线程可用。在一个更传统的线程化web应用程序中,这似乎是一个糟糕的默认,当期货正在执行一个任务,如等待一个数据库(而不是某些cpu绑定任务)。

When using futures in scala the default behaviour is to use the default Implicits.global execution context. It seems this defaults to making one thread available per processor. In a more traditional threaded web application this seems like a poor default when the futures are performing a task such as waiting on a database (as opposed to some cpu bound task).

我期望覆盖默认上下文在生产中会是相当标准的,但我可以找到这么少的文档,看起来它似乎可能不是很常见。我缺少一些东西?

I'd expect that overriding the default context would be fairly standard in production but I can find so little documentation about doing it that it seems that it might not be that common. Am I missing something?

推荐答案

为什么不要问我应该使用多个执行上下文为不同的东西?如果这是问题,那么我的答案是肯定的。我在哪里工作,我们使用Akka。在我们的应用程序中,我们使用默认的Akka执行上下文来实现非阻塞功能。然后,因为目前没有良好的非阻塞jdbc驱动程序,所有我们的阻塞SQL调用使用单独的执行上下文,其中我们有一个线程每连接方法。保持主执行上下文(一个fork连接池)没有阻塞导致我们的吞吐量显着增加。

Instead of thinking of it as overriding the default execution context, why not ask instead "Should I use multiple execution contexts for different things?" If that's the question, then my answer would be yes. Where I work, we use Akka. Within our app, we use the default Akka execution context for non blocking functionality. Then, because there is no good non blocking jdbc driver currently, all of our blocking SQL calls use a separate execution context, where we have a thread per connection approach. Keeping the main execution context (a fork join pool) free from blocking lead to a significant increase in throughput for us.

我认为使用多个不同的执行上下文是完全确定的为您的系统中的不同类型的工作。它对我们工作得很好。

I think it's perfectly ok to use multiple different execution contexts for different types of work within your system. It's worked well for us.

这篇关于我应该覆盖默认的ExecutionContext吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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