Project

General

Profile

Feature #13527

Accept IO object as stdin data in Open3.capture

Added by janko (Janko Marohnić) about 2 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
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

Associated revisions

Revision 3d945399
Added by akr (Akira Tanaka) over 1 year ago

lib/open3.rb: accept IO-like object for :stdin_data argument.

Open3.capture3, Open3.capture2, Open3.capture2e accepts
IO-like object for :stdin_data argument.
[ruby-core:80936] [Feature #13527] proposed by janko.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 60236
Added by akr (Akira Tanaka) over 1 year ago

lib/open3.rb: accept IO-like object for :stdin_data argument.

Open3.capture3, Open3.capture2, Open3.capture2e accepts
IO-like object for :stdin_data argument.
[ruby-core:80936] [Feature #13527] proposed by janko.

Revision 60236
Added by akr (Akira Tanaka) over 1 year ago

lib/open3.rb: accept IO-like object for :stdin_data argument.

Open3.capture3, Open3.capture2, Open3.capture2e accepts
IO-like object for :stdin_data argument.
[ruby-core:80936] [Feature #13527] proposed by janko.

Revision 60236
Added by akr (Akira Tanaka) over 1 year ago

lib/open3.rb: accept IO-like object for :stdin_data argument.

Open3.capture3, Open3.capture2, Open3.capture2e accepts
IO-like object for :stdin_data argument.
[ruby-core:80936] [Feature #13527] proposed by janko.

History

Updated by shyouhei (Shyouhei Urabe) about 2 years ago

  • Assignee set to akr (Akira Tanaka)
  • Status changed from Open to Assigned
#2

Updated by akr (Akira Tanaka) over 1 year ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r60236.


lib/open3.rb: accept IO-like object for :stdin_data argument.

Open3.capture3, Open3.capture2, Open3.capture2e accepts
IO-like object for :stdin_data argument.
[ruby-core:80936] [Feature #13527] proposed by janko.

Updated by janko (Janko Marohnić) over 1 year ago

Thank you for the patch!

Since IO.copy_stream also accepts IO objects that respond only to #read (and not #readpartial), would it be possible to also permit those objects as :stdin_data (maybe check that the object responds to either #read or #readpartial)?

That was my use case, being able to pass any #read-able object as the standard input. In my case the IO-like objects I work with don't respond to #readpartial, because #read is enough.

Also available in: Atom PDF