代码之家  ›  专栏  ›  技术社区  ›  Robin

使用命名管道时出现“打开的文件太多”错误

  •  2
  • Robin  · 技术社区  · 6 年前

    我正在运行两个脚本,过了一会儿,我 Too many open files @ error/blob.c/ImageToFile/1832 .

    第一个脚本的简化版本。它应该读取写入图像管道的图像,对其进行处理,然后将其写入OCR管道以供OCR读取。

    # creates 2 named pipes
    File.mkfifo(image_pipe) rescue nil
    File.mkfifo(ocr_pipe) rescue nil
    
    while image = Image.read(image_pipe)
      # do some stuff with `image`...
    end
    

    第二个脚本使用ffmpeg从视频中提取帧,并将其写入 image_pipe

    # image_pipe is the same as the script above.
    
    (14..movie.duration).step(0.5) do
      `/usr/local/bin/ffmpeg [some options...] #{image_pipe}`
    end
    

    我认为问题在于,在读取第一个脚本循环中的图像时,rmagic打开了太多的文件描述符,但我不确定如何阻止这种情况发生。这个 Magick::Image 班级没有 close 方法或任何东西,阿法克。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Robin    6 年前

    我没有找到问题的根源,但是 ulferts 帮助我找到一个我可以接受的解决方案。

    我们不应该让rmagic打开文件本身,而是应该在自己的方面处理它,然后使用 .from_blob 创建 Magick::Image 实例。

    while f = File.read(image_pipe)
      image = Image.from_blob(f)
      # ... do stuff with image.
    end