4

I am using AWS SDK to access files in S3. However, the S3 operations from AWS SDK are synchronous. Is it possible to wrap such synchronous operations to make them asynchronous in Scala?

Wrapping with Future is not a right answer since that operation will block in another thread.

Khanetor
  • 11,595
  • 8
  • 40
  • 76

1 Answers1

3

Something that I've been using for a while:

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 } }
}

So, as you said - yes, Futures might block, that's why we use special blocking construct.

More about this:

Community
  • 1
  • 1
sap1ens
  • 2,877
  • 1
  • 27
  • 30
  • From the doc it says the `blocking` construct is "Used to designate a piece of code which potentially blocks, allowing the current BlockContext to adjust the runtime's behavior. Properly marking blocking code may improve performance or avoid deadlocks." Do you know what it will do when the code does block? What does it mean by BlockingContext adjust the runtime's behavior? – Khanetor Jul 05 '15 at 03:33
  • Have you checked those links that I posted? They are really helpful, especially Google Group discussion. It'll be much clear after. – sap1ens Jul 05 '15 at 03:36
  • 1
    To add more detail to the answer, the `blocking` construct will signal the `ExecutionContext` that the code might block, so that the `ExecutionContext` can ***temporarily*** spawn more thread than configured. This is detailed in chapter 4, section "Blocking in asynchronous computations" of the book Learning Concurrent with Scala. – Khanetor Aug 12 '15 at 14:43