Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does scala offer async non-blocking IO when working with files?

I am using scala 2.10 and I wonder If there is some package which has async IO when working with files?

I did some search o this topic but mostly found examples as following

val file = new File(canonicalFilename)
val bw = new BufferedWriter(new FileWriter(file))
bw.write(text)
bw.close()

what essentially essentially java.io package with blocking IO operations - write, read etc. I also found scala-io project with this intention but it seems that project is dead last activity 2012.

What is best practice in this scenario? Is there any scala package or the common way is wrapping java.io code to Futures and Observables ?

My use case is from an Akka actor need to manipulate files on local or remote file system. Need to avoid blocking. Or is there any better alternative?

Thnaks for clarifing this

like image 498
jaksky Avatar asked Jan 01 '15 21:01

jaksky


2 Answers

Scala does not offer explicit API for asynchronous file IO, however the plain Java API is exactly the right thing to use in those cases (this is actually a good thing, we can use all these nice APIs without any wrapping!). You should look into using java.nio.channels.AsynchronousFileChannel, which is available since JDK7 and makes use of the underlying system async calls for file IO.

Akka IO, while not providing file IO in it's core, has a module developed by Dario Rexin, which allows to use AsynchronousFileChannel with Akka IO in a very simple manner. Have a look at this library to make use of it: https://github.com/drexin/akka-io-file

In the near future Akka will provide File IO in its akka-streams module. It may be as an external library for a while though, we're not exactly sure yet where to put this as it will require users to have JDK at-least 7, while most of Akka currently supports JDK6. Having that said, streams based asynchronous back-pressured file IO is coming soon :-)

like image 66
Konrad 'ktoso' Malawski Avatar answered Oct 12 '22 17:10

Konrad 'ktoso' Malawski


If you're using scalaz-stream for your async support it has file functionality that's built on the java.nio async APIs - that's probably the approach I'd recommend. If you're using standard scala futures possibly you can use akka-io? which I think uses Netty as a backend. Or you can call NIO directly - it only takes a couple of lines to adapt a callback-based API to scalaz or scala futures.

like image 34
lmm Avatar answered Oct 12 '22 19:10

lmm