New Discussion

Notifications

You’re not receiving notifications from this thread.

Spawn process and send data to STDIN when data avilable

0
Ruby

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.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.