Project

General

Profile

Actions

Feature #13527

closed

Accept IO object as stdin data in Open3.capture

Added by janko (Janko Marohnić) almost 7 years ago. Updated about 6 years ago.

Status:
Closed
Target version:
-
[ruby-core:80936]

Description

Currently Open3.capture3, Open3.capture2, Open3.capture2e accept a :stdin_data option, which allows you to write a String into subprocess' standard input. This patch adds the ability to also pass in an IO-like object (any object that respond to #read) as :stdin_data, which will them be streamed to standard input.

Open3.capture3("file", "--mime-type", "--brief", "-", stdin_data: File.open("image.jpg"))
Open3.capture3("ffprobe", "-print_format", "json", "-i", "pipe:0", stdin_data: File.open("video.mp4"))

This is convenient when you want to pass in files into standard input (images, videos etc), because this way you don't have to load the whole file into memory, the file contents will get efficiently streamed into subprocess' standard input.

Another advantage is that many command line tools will stop reading the standard input once they get enough data. In both the examples above the subprocess will stop reading standard input as soon as it gets the information it needs (the image MIME type or video metadata), and in both examples it turns out to be about 1-2MB. This isn't that useful if the IO object represents a file on the filesystem (where reading is fast), but it becomes very useful when the IO object represents a file from the database or a remote file over HTTP. That way you don't need to guess how much data the subprocess needs, you can just give it the IO object and it will read as much as it needs, and then only that amount will be retrieved from the database or downloaded from the Internet.


Files

open3-accept-stdin-data-as-io.patch (6.09 KB) open3-accept-stdin-data-as-io.patch janko (Janko Marohnić), 04/30/2017 11:49 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0