Spawn process and send data to STDIN when data avilable
I want to spawn a process (golang binary), capture its STDOUT and have a reference to STDIN which i can send to at any time. In my case i am getting data in via Websockets and want to pass them to STDIN of the spawned process.
I am having issues reusing STDIN with various methods i tried, like using 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"
I tried additionally to use a lock syntax with PTY and popen3 but both failed to receive STDIN.