Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:17:35+00:00 2026-05-30T08:17:35+00:00

I would like to use Sinatra’s Streaming capability introduced in 1.3 coupled with some

  • 0

I would like to use Sinatra’s Streaming capability introduced in 1.3 coupled with some stdout redirection. It would basically be a live streaming output of a long running job. I looked into this question and the Sinatra streaming sample in the README.

Running 1.8.7 on OSX:

require 'stringio'
require 'sinatra'

$stdout.sync = true

module Kernel
  def capture_stdout
    out = StringIO.new
    $stdout = out
    yield out
  ensure
    $stdout = STDOUT
  end
end

get '/' do
  stream do |out|
    out << "Part one of a three part series... <br>\n"
    sleep 1
    out << "...part two... <br>\n"
    sleep 1
    out << "...and now the conclusion...\n"

    Kernel.capture_stdout do |stream|
        Thread.new do
            until (line = stream.gets).nil? do
               out << line
             end
        end
        method_that_prints_text
    end
  end
end

def method_that_prints_text
    puts "starting long running job..."
    sleep 3
    puts "almost there..."
    sleep 3
    puts "work complete!"
end

So this bit of code prints out the first three strings properly, and blocks while the method_that_prints_text executes and does not print anything to the browser. My feeling is that stdout is empty on the first call and it never outputs to the out buffer. I’m not quite sure what the proper ordering would be and would appreciate any suggestions.

I tried a few of the EventMachine implementations mentioned in the question above, but couldn’t get them to work.

UPDATE

I tried something slightly different to where I had the method run in a new thread, and override STDOUT for that thread as described here…

Instead of Kernel.capture_stdout above…

s = StringIO.new

Thread.start do
    Thread.current[:stdout] = s
    method_that_prints_text
end.join

while line = s.gets do
    out << line
end

out << s.string

With the ThreadOut module listed in the link above, this seems to work a bit better. However it doesn’t stream. The only time something is printed to the browser is on the final line out << s.string. Does StringIO not have the capability to stream?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:17:36+00:00Added an answer on May 30, 2026 at 8:17 am

    I ended up solving this by discovering that s.string was updated periodically as time went on, so I just captured the output in a separate thread and grabbed the differences and streamed them out. It appears as though string redirection doesn’t behave like a normal IO object.

    s = StringIO.new
    t = Thread.start do
      Thread.current[:stdout] = s
      method_that_prints_text
      sleep 2
    end
    
    displayed_text = ''
    while t.alive? do
      current_text = s.string
      unless (current_text.eql?(displayed_text))
        new_text = current_text[displayed_text.length..current_text.length]
        out << new_text
        displayed_text = current_text * 1
      end
      sleep 2   
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use Emacs to edit some VB6 files but Emacs does
My use case is that I would like to do error handling in sinatra.
We are using Datamapper in a Sinatra application and would like to use case
I would like to use a lighter framework than Rails (Sinatra/Ramaze/Camping) but am concerned
I currently have some large strings that I would like use as test data
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.