首页 > 解决方案 > 当数据可用时生成进程并将数据发送到 STDIN

问题描述

我想生成一个进程(golang 二进制文件),捕获它的 STDOUT 并引用我可以随时发送到的 STDIN。在我的情况下,我通过 Websockets 获取数据,并希望将它们传递给生成进程的 STDIN。

我在尝试使用各种方法重用 STDIN 时遇到问题,例如使用PTY.spawn

input = nil

def run
  cmd = "app/bins/transcode" 
  read, write, pid = PTY.spawn(cmd)
  input = write
  Signal.trap(:WINCH) { write.winsize = STDOUT.winsize }
  
  read.each do |line|
    # THis works fine and sends the spawned process' STDOUT over websockets
    ActionCable.server.broadcast 'shell_channel', test: line
  end
  Process.wait(pid)
end

# This is never sent to STDIN of the spawned process
input.puts "hello"

我还尝试对 PTY 和 popen3 使用锁定语法,但都未能接收到 STDIN。

标签: ruby-on-railsrubychild-processactioncable

解决方案


推荐阅读