在基于 EventMachine 的应用程序中读取文件的最佳方式是什么?
为了不阻塞反应器,我想异步读取文件,但我没有找到使用 EventMachine 的明显方法。我尝试了几种不同的方法,但没有一个感觉是正确的:
- 只要读取文件,它就会阻塞反应堆,但管它呢,它并没有那么慢(除非它是一个大文件) ,那么它肯定是)。
- 打开文件进行读取,并在每个刻度上读取一个块(但是读取多少?太多,它会阻塞反应器,太少,读取速度会比必要的慢)。
EM.popen('cat some/file', FileReader)
感觉真的很奇怪,但比上面的替代方案效果更好。与 LineAndTextProtocol 结合使用,它可以非常快速地读取行。EM.attach
,但我还没有找到任何如何使用它的示例,而且我在邮件列表中发现的唯一内容是它已被弃用,取而代之的是......EM。 watch
,我没有找到如何使用它来读取文件的示例。
如何读取 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:
- 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 theLineAndTextProtocol
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.
How do you read files within a EventMachine reactor loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EM.attach/watch 不能用于文件,因为基于磁盘的文件描述符上的 select/epoll 将始终返回可读。
最终,这取决于您想要做什么。如果是小文件,直接File.read即可。如果它较大,您可以随着时间的推移读取小块。例如,EM::FileStreamer 这样做是为了通过网络发送大文件。
另一个常见的用例是尾部文件并在文件更改时读取新内容。这可以使用 EM.watch_file 来实现: http://github.com/jordansissel/eventmachine-tail
EM.attach/watch cannot be used on files, as select/epoll on a disk-based file descriptor will always return readable.
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.
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