What is the best way to read files in an EventMachine-based app?

Posted by Theo on Stack Overflow See other posts from Stack Overflow or by Theo
Published on 2010-05-01T09:59:31Z Indexed on 2010/05/01 10:07 UTC
Read the original article Hit count: 220

Filed under:
|
|

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 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.

How do you read files within a EventMachine reactor loop?

© Stack Overflow or respective owner

Related posts about eventmachine

Related posts about ruby