Created
April 22, 2010 21:28
-
-
Save ncr/375843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Net::HTTPResponse streaming for use in Rack apps with Fibers! | |
def read_body # Net::HTTPResponse#read_body | |
yield "foo"; yield "bar"; yield "baz" | |
end | |
class R # A Rack Streaming response | |
def initialize | |
@f = Fiber.new do | |
read_body do |chunk| # Net::HTTPResponse#read_body | |
sleep(0.5) | |
Fiber.yield(chunk) | |
end | |
end | |
end | |
def each | |
while x = @f.resume | |
yield x | |
end | |
end | |
end | |
R.new.each do |chunk| | |
puts chunk # Streaming! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment