我运行2个脚本,一段时间后,我越来越Too many open files @ error/blob.c/ImageToFile/1832
。
简体版第一脚本。它应该读取写入image_pipe图像,对其进行处理,并将其写入ocr_pipe的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
我认为这个问题是RMagick开太多的文件描述符在第一个脚本的循环读取的图像时,但我不知道如何阻止这种情况发生。该Magick::Image
类没有close
方法什么的,据我所知。
我没有找到问题的根源,但ulferts帮我找到一个变通方法,是可以接受我。
与其让RMagick打开文件本身,我们应该处理它在我们的身边,然后用.from_blob
创建Magick::Image
实例。
while f = File.read(image_pipe)
image = Image.from_blob(f)
# ... do stuff with image.
end