【问题标题】:sending message to subprocess with Ruby使用 Ruby 向子进程发送消息
【发布时间】:2016-02-25 03:21:57
【问题描述】:

假设我有一个从 ruby​​ 脚本开始的进程,如下所示:

pid = spawn('./my_awesome_process')
Process.detach(pid)

此进程在 STDIN 上侦听。 我现在希望读取命令行输入,并将其发送到这样的进程:

puts "Please enter input"
input = gets

然后我如何将输入转发到我的流程?

【问题讨论】:

标签: ruby process subprocess


【解决方案1】:

您可以使用IO.popen,它将子进程的stdinstdout 附加到IO 对象。

IO.popen('rev', 'r+') do |io|
  io.puts "I'm looking at the man in the mirror"
  io.close_write
  puts io.gets
end
rorrim eht ni nam eht ta gnikool m'I

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 2016-07-19
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多