ruby - 使用 Ruby 向子进程发送消息

标签 ruby process subprocess

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

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

这个进程监听 STDIN。 我现在希望读取命令行输入,并像这样将其发送到进程:

puts "Please enter input"
input = gets

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

最佳答案

您可以使用 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

关于ruby - 使用 Ruby 向子进程发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863943/

相关文章:

ruby-on-rails - block 有什么用?

ruby-on-rails - 为什么在使用路由辅助方法时必须使用括号?

java - 子进程继承了什么?

c# - 如果某个进程在没有计时器的情况下运行,请执行某些操作

python - `subprocess.call` 与直接在 shell 中运行命令的操作不同

ruby - 如何配置 asciidoctor-gradle-plugin 来处理 plantuml?

ruby - Hash 的 compare_by_identity 是如何工作的?

node.js - 使用递归process.nexttick是否可以使其他进程或线程正常工作?

python - 在 Linux 上调用 virtualenv bundled pip wuth subprocess.call 在到 venv 目录的长路径上失败

python - 如何在将数据写入标准输入的 Python 中执行进程?