使用文件时,scala是否提供异步非阻塞IO? [英] Does scala offer async non-blocking IO when working with files?

查看:85
本文介绍了使用文件时,scala是否提供异步非阻塞IO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scala 2.10,我想知道是否有一些在处理文件时具有异步IO的软件包?

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()

本质上是具有阻塞IO操作的java.io程序包-写入,读取等。我还发现 scala-io 有此意图的项目,但似乎该项目在2012年的最后一次活动中已死。

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.

在这种情况下,最佳做法是什么?是否有任何scala软件包或将java.io代码包装到Futures和Observables中的常用方法?

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 ?

我的用例来自Akka演员,需要在本地或本地操作文件远程文件系统。需要避免阻塞。还是有更好的选择?

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?

对此澄清的想法

推荐答案

Scala 没有提供用于异步文件IO的显式API,但是在这种情况下,纯Java API正是正确的选择(这实际上是一件好事,我们可以使用所有这些不错的API,而无需任何包装!)。您应该研究使用 java.nio.channels。 AsynchronousFileChannel ,自JDK7起可用,并利用对文件IO的底层系统异步调用。

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 ,而它的核心没有提供文件IO,而是由Dario Rexin开发的模块,该模块允许以非常简单的方式将AsynchronousFileChannel与Akka IO一起使用。看看这个库可以利用它: https://github.com/drexin/akka -io-file

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

在不久的将来 Akka将在其 akka-streams 模块。虽然它可能作为外部库已有一段时间了,但我们尚不确定要在哪里放置它,因为它将要求用户至少具有JDK 7,而大多数Akka当前都支持JDK6。话虽如此,基于流的异步背压文件IO即将推出:-)

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 :-)

这篇关于使用文件时,scala是否提供异步非阻塞IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆