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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With