我写了一个小脚本,用 Sinatra 和 Thin 逐字节流式传输文件:
#!/usr/bin/env ruby
require 'sinatra'
require "sinatra/streaming"
get '/' do
stream do |out|
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
break if out.closed?
putc byte.chr
out << byte.chr unless out.closed?
end
end
end
end
这按预期工作:
% ruby streamer.rb
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
% curl http://127.0.0.1:4567
并且文件内容两边都写到stdout
如果我删除该行putc byte.chr
,则服务器崩溃并被杀死kill -9
如果我将此脚本更改为模块化样式并添加一个config.ru
:
#!/usr/bin/env ruby
require 'sinatra/base'
require "sinatra/streaming"
class TestStreamer < Sinatra::Base
helpers Sinatra::Streaming
get '/' do
stream do |out|
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
break if out.closed?
# putc byte.chr
out << byte.chr unless out.closed?
end
end
end
end
end
配置.ru:
$:.unshift(File.join(File.dirname(__FILE__)))
require 'rubygems'
require 'sinatra/base'
require 'streamer_modular'
map '/' do
run TestStreamer
end
它启动:
% thin start
>> Using rack adapter
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
但是当我发出请求时,文件在双方的发送速度都非常慢,并且在 30 秒后关闭。在这种情况下,Sinatra 不会崩溃。
% time curl -v http://127.0.0.1:3000
* About to connect() to 127.0.0.1 port 3000 (#0)
* Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
> Host: 127.0.0.1:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Frame-Options: sameorigin
< X-XSS-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Connection: close
< Server: thin 1.4.1 codename Chromeo
<
* Closing connection #0
This package was cre
curl -v http://127.0.0.1:3000 0,01s user 0,00s system 0% cpu 32,559 total
当我删除流助手时,文件会立即写入服务器端的标准输出:
#!/usr/bin/env ruby
require 'sinatra/base'
require "sinatra/streaming"
class TestStreamer < Sinatra::Base
helpers Sinatra::Streaming
get '/' do
# stream do |out|
begin
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
# break if out.closed?
putc byte.chr
# out << byte.chr unless out.closed?
end
end
end
end
end
% ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
% gem list
*** LOCAL GEMS ***
backports (2.6.2)
bundler (1.1.5, 1.1.3)
daemons (1.1.8)
eventmachine (0.12.10)
rack (1.4.1)
rack-protection (1.2.0)
rack-test (0.6.1)
sinatra (1.3.2)
sinatra-contrib (1.3.1)
thin (1.4.1)
tilt (1.3.3)
% gem update
Updating installed gems
Nothing to update
% ls -al /usr/share/doc/ia32-libs/copyright
-rw-r--r-- 1 root root 607403 2. Jan 2012 /usr/share/doc/ia32-libs/copyright
% head -n2 /usr/share/doc/ia32-libs/copyright
This package was created by Daniel Jacobowitz <dan@debian.org> on Sun, Aug
8th, 2004. It was based on the ia32-libs package by Bdale Garbee
我错过了什么吗?你能重现这个,也许能为我找到一个解决方法吗?