Sławosz
Sławosz

Reputation: 11687

$stdout.gets blocks when it should return output, why?

I have such server schema:

sleep 5
puts 'Server started'
loop { }

When I run it in irb:

arr = Open3.popen3('ruby server.rb') arr[1].gets

the gets blocks, and even I know it should return 'Server started' it does not. When I interrupt it and call arr[1].gets again, it returns 'Server started' immediately.

How to make it return output on first arr[1].gets?

PS. When I remove loop { } from server it works perfectly.

Upvotes: 1

Views: 142

Answers (1)

Stefan
Stefan

Reputation: 114218

You have to to either flush your output buffer:

puts 'Server started'
$stdout.flush

Or enable sync mode:

$stdout.sync = true
puts 'Server started'  # flushed automatically

Upvotes: 1

Related Questions