All threads / Spawn process and send data to STDIN when data avilable

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Spawn process and send data to STDIN when data avilable

Drazen Mokic asked in 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

Want to stay up-to-date with Ruby on Rails?

Join 51,723+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.