Slick with Play and Execution 上下文
Slick with Play and Execution Context
阅读 Play Framework 文档,明确指出在使用阻塞 I/O 操作或 CPU 密集操作时,应该将这些操作放在另一个 ExecutionContext 上。他们还明确建议在数据库示例中这样做。
但是阅读 Slick 文档时他们也说 Slick 在 ExecutionContext 上用于查询并且所有操作都是异步的。
问题,在默认 ExecutionContext 下可以在 Play 中使用 Slick 吗?
编辑:Scala。
在您可以在应用程序属性文件中配置的小型 hicariCP
池中巧妙地完成所有 IO 工作。它只需要 ExecutionContext
用于 DB 调用之间的操作。例如,当您在 DBIOAction
上调用 map
方法时。来自 slick documentation:
If an action depends on a previous action in the sequence, you have to
compute it on the fly with flatMap or map. These two methods plus
filter enable the use of for comprehensions for action sequencing.
Since they take function arguments, they also require an implicit
ExecutionContext on which to run the function. This way Slick ensures
that no non-database code is run on the database thread pool.
因此,在您不在此方法中执行任何阻塞操作之前,使用默认播放是安全的ExecutionContext
。
阅读 Play Framework 文档,明确指出在使用阻塞 I/O 操作或 CPU 密集操作时,应该将这些操作放在另一个 ExecutionContext 上。他们还明确建议在数据库示例中这样做。
但是阅读 Slick 文档时他们也说 Slick 在 ExecutionContext 上用于查询并且所有操作都是异步的。
问题,在默认 ExecutionContext 下可以在 Play 中使用 Slick 吗?
编辑:Scala。
在您可以在应用程序属性文件中配置的小型 hicariCP
池中巧妙地完成所有 IO 工作。它只需要 ExecutionContext
用于 DB 调用之间的操作。例如,当您在 DBIOAction
上调用 map
方法时。来自 slick documentation:
If an action depends on a previous action in the sequence, you have to compute it on the fly with flatMap or map. These two methods plus filter enable the use of for comprehensions for action sequencing. Since they take function arguments, they also require an implicit ExecutionContext on which to run the function. This way Slick ensures that no non-database code is run on the database thread pool.
因此,在您不在此方法中执行任何阻塞操作之前,使用默认播放是安全的ExecutionContext
。