什么是基于EventMachine的应用读取文件的最好方法? [英] What is the best way to read files in an EventMachine-based app?

查看:146
本文介绍了什么是基于EventMachine的应用读取文件的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了不阻挡反应器,我想异步读取文件,但我发现使用EventMachine的做的没有明显的方式。我已经尝试了几种不同的方法,但他们没有感觉不错:

In order not to block the reactor I would like to read files asynchronously, but I've found no obvious way of doing it using EventMachine. I've tried a few different approaches, but none of them feels right:


  • 只需读取该文件,它会阻止反应堆,但什么是地狱,它不是的的慢(除非它是一个大的文件,然后将它肯定是)。

  • 打开文件进行读取,并读取每个剔一大块(但多少看?太多了,它会阻止反应堆,太少和阅读将超过必要降低)。

  • EM.popen('猫一些/文件',的FileReader)感觉很不可思议,但工程比上述方案更好。与 LineAndTextProtocol 读取线pretty迅速。
  • 组合
  • EM.attach ,但我还没有找到如何使用它的例子,我已经在邮件列表上找到的唯一的事情是,它是德$ p $赞成... pcated

  • EM.watch ,其中我发现不知道如何使用阅读文件的示例。

  • Just read the file, it'll block the reactor, but what the hell, it's not that slow (unless it's a big file, and then it definitely is).
  • Open the file for reading and read a chunk on each tick (but how much to read? too much and it'll block the reactor, too little and reading will get slower than necessary).
  • EM.popen('cat some/file', FileReader) feels really weird, but works better than the alternatives above. In combination with the LineAndTextProtocol it reads lines pretty swiftly.
  • EM.attach, but I haven't found any examples of how to use it, and the only thing I've found on the mailing list is that it's deprecated in favour of…
  • EM.watch, which I've found no examples of how to use for reading files.

你怎么一个EventMachine的反应器循环中读取文件?

How do you read files within a EventMachine reactor loop?

推荐答案

EM.attach /手表不能在文件中使用,作为一个基于磁盘的文件描述符选择/ epoll的总是会返回可读的。

EM.attach/watch cannot be used on files, as select/epoll on a disk-based file descriptor will always return readable.

归根结底,这取决于你想要做什么。如果它是一个小文件,只需File.read它。如果是较大的,可以随着时间的推移读取小块。例如,EM :: FileStreamer这样做是为了通过网络发送大文件。

Ultimately, it depends on what you're trying to do. If it's a small file, just File.read it. If it is larger, you can read small chunks over time. For example, EM::FileStreamer does this to send large file over the network.

另一种常见的用例是尾文件,并在新的阅读内容时,它改变了。这可以使用EM.watch_file实现:<一href=\"http://github.com/jordansissel/eventmachine-tail\">http://github.com/jordansissel/eventmachine-tail

Another common use-case is to tail a file and read in new contents when it changes. This can be achieved using EM.watch_file: http://github.com/jordansissel/eventmachine-tail

这篇关于什么是基于EventMachine的应用读取文件的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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