如何使用 Ruby 和 IO.popen 写入和读取进程?
我写了这个,但它不起作用......
output = IO.popen("irb", "r+") do |pipe| pipe.gets pipe.puts "10**6" pipe.gets pipe.puts "quit" end
我重写了
IO.popen("irb", "w+") do |pipe| 3.times {puts pipe.gets} # startup noise pipe.puts "10**6\n" puts pipe.gets # I expect " => 1000000" pipe.puts "quit" # I expect exit from irb endbut It didn`t work too
I wrote this, but it didn`t work...
output = IO.popen("irb", "r+") do |pipe| pipe.gets pipe.puts "10**6" pipe.gets pipe.puts "quit" end
I rewrite so
IO.popen("irb", "w+") do |pipe| 3.times {puts pipe.gets} # startup noise pipe.puts "10**6\n" puts pipe.gets # I expect " => 1000000" pipe.puts "quit" # I expect exit from irb end
but It didn`t work too
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,上面的示例将挂起,因为管道仍然打开用于写入,并且您调用的命令(Ruby 解释器)需要更多命令/数据。
另一个答案将
__END__
发送到 ruby - 这在这里有效,但这个技巧当然不适用于您可能通过popen
调用的任何其他程序。当您使用
popen
时,您需要使用IO#close_write
关闭管道。请参阅:Ruby 3.1 popen*
更多详细信息:
在 Ruby 中,
IO.popen
、IO.popen2
、IO.popen2e
和IO.popen3
是用于处理子进程的方法,它们的不同之处在于处理输入、输出和错误流的方式。以下是对差异以及何时使用每个差异的解释:IO.popen
:IO.popen
是一种多功能方法,允许您创建子进程并与其标准输入和输出交互。IO.popen2
:IO.popen2
创建一个带有单独管道的子进程,用于标准输入和输出。IO.popen2e
:IO.popen2e
与IO.popen2
类似,但它将标准输出和标准错误流合并到单个流中。IO.popen3
:IO.popen3
创建一个子进程,其中包含用于标准输入、标准输出和标准错误的单独管道。何时使用每个版本取决于您的具体要求:
当您需要与子进程的输入和输出交互并且不需要单独处理错误消息时,请使用
IO.popen
。当您想要与标准输入分开捕获标准输出并需要向进程发送数据时,请使用
IO.popen2
。当您想要在单个流中同时捕获标准输出和标准错误时,请使用 IO.popen2e。
当您需要单独的管道用于标准输入、标准输出和标准错误,并且希望与子进程交互并分别捕获输出和错误消息时,请使用 IO.popen3。
In general the above example will hang because the pipe is still open for writing, and the command you called (the ruby interpreter) expects further commands / data.
The other answer sends
__END__
to ruby -- this works here, but this trick will of course not work with any other programs you might call viapopen
.When you use
popen
you need to close the pipe withIO#close_write
.See: Ruby 3.1 popen*
In more Detail:
In Ruby,
IO.popen
,IO.popen2
,IO.popen2e
, andIO.popen3
are methods used for working with subprocesses, and they differ in how they handle input, output, and error streams. Here's an explanation of the differences and when to use each:IO.popen
:IO.popen
is a versatile method that allows you to create a subprocess and interact with its standard input and output.IO.popen2
:IO.popen2
creates a subprocess with separate pipes for standard input and output.IO.popen2e
:IO.popen2e
is similar toIO.popen2
, but it combines the standard output and standard error streams into a single stream.IO.popen3
:IO.popen3
creates a subprocess with separate pipes for standard input, standard output, and standard error.When to use each version depends on your specific requirements:
Use
IO.popen
when you need to interact with a subprocess's input and output and don't require separate handling of error messages.Use
IO.popen2
when you want to capture the standard output separately from the standard input and need to send data to the process.Use
IO.popen2e
when you want to capture both standard output and standard error together in a single stream.Use
IO.popen3
when you need separate pipes for standard input, standard output, and standard error, and you want to interact with the subprocess and capture both output and error messages separately.要么做
,要么做
Either do
or do