scala/basic

Blocking Future

wefree 2023. 2. 13. 11:16

scala-best-practices 의 MUST use Scala's BlockContext on blocking I/O 문서와 SHOULD use a separate thread-pool for blocking I/O 를 참고했을 때, 간편하게 쓴다면 아래처럼 BlockingFuture Helper 를 만드는 것도 좋을 것 같다.

 

import scala.concurrent.{blocking, Future, ExecutionContext}
/**
 * This is an idiomatic way of executing blocking code
 * Use BlockingFuture(...) instead of normal Future(...) anywhere
 */
object BlockingFuture {
    def apply[T](body: => T)(implicit execctx: ExecutionContext): Future[T] = Future { blocking { body } }
}

참고: https://stackoverflow.com/a/31226889