【问题标题】:How to read and process a file in Ruby with EventMachine如何使用 EventMachine 在 Ruby 中读取和处理文件
【发布时间】:2013-04-26 00:20:42
【问题描述】:

我想知道是否有人想出了一种使用 EventMachine 处理更大 (500Mb+) 文本文件的方法,而您实际上需要访问各个行。

【问题讨论】:

    标签: ruby file eventmachine


    【解决方案1】:

    我想我找到了答案,唯一的问题是 read_chunk 在 io.gets 之后被调用,我不确定它为什么起作用:)

    require 'eventmachine'
    def process_line(line)
      puts line
    end
    EM.run do
      io = File.open('query_profiles.csv')
      read_chunk = proc do
        if line = io.gets
          process_line(line)
          EM.next_tick(read_chunk)
        else
          EM.stop
        end
      end
      EM.next_tick(read_chunk)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多